Replace stub for mongoDB lesson (create many records) (#36492)

* Replace stub with hints and solution

For "create many records" mongoDB lesson

* Fix markdown for inline code

Add new line to allow markdown to render inline code

* Use callback in `createAndSavePerson` function

* fix: corrected hints syntax
This commit is contained in:
Ai-Lyn Tang
2019-07-24 02:58:57 +10:00
committed by Randell Dawson
parent 935492530a
commit 03dce61f72

View File

@ -1,10 +1,65 @@
---
title: Create Many Records with model.create()
---
## Create Many Records with model.create()
# Create Many Records with model.create()
This is a stub. <a href='https://github.com/freecodecamp/guides/tree/master/src/pages/certifications/apis-and-microservices/mongodb-and-mongoose/create-many-records-with-model.create/index.md' target='_blank' rel='nofollow'>Help our community expand it</a>.
## Hints
<a href='https://github.com/freecodecamp/guides/blob/master/README.md' target='_blank' rel='nofollow'>This quick style guide will help ensure your pull request gets accepted</a>.
### Hint #1
Create an array of objects. Each object has a name, age and an array of favorite foods. Use the variable name `arrayOfPeople`.
<!-- The article goes here, in GitHub-flavored Markdown. Feel free to add YouTube videos, images, and CodePen/JSBin embeds -->
### Hint #2
Create your many records (ie. create your people) inside the callback for `createManyPeople`.
### Hint #3
Use `Model.create()` to create many records. You should replace `Model` with the name of the model you defined in the previous section. Most likely you called your model `Person`.
### Hint #4
The `Model.create` function requires a callback, similar to the `person.save` function you used in the previous section.
## Solutions
<details><summary>Solution #1 (Click to Show/Hide)</summary>
Code for `myApp.js`
```javascript
/** 1) Install & Set up mongoose */
const mongoose = require('mongoose');
mongoose.connect(process.env.MONGO_URI);
/** 2) Create a 'Person' Model */
var personSchema = new mongoose.Schema({
name: String,
age: Number,
favoriteFoods: [String]
});
/** 3) Create and Save a Person */
var Person = mongoose.model('Person', personSchema);
var createAndSavePerson = function(done) {
var janeFonda = new Person({name: "Jane Fonda", age: 84, favoriteFoods: ["vodka", "air"]});
janeFonda.save(function(err, data) {
if (err) return console.error(err);
done(null, data)
});
};
/** 4) Create many People with `Model.create()` */
var arrayOfPeople = [
{name: "Frankie", age: 74, favoriteFoods: ["Del Taco"]},
{name: "Sol", age: 76, favoriteFoods: ["roast chicken"]},
{name: "Robert", age: 78, favoriteFoods: ["wine"]}
];
var createManyPeople = function(arrayOfPeople, done) {
Person.create(arrayOfPeople, function (err, people) {
if (err) return console.log(err);
done(null, people);
});
};
```
</details>