mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-04-24 01:00:51 -04:00
Replace stub for mongDB lesson (use model.findOne) (#36493)
* Replace stub for mongDB lesson (use model.findOne) Add solution * Use callback in `createAndSavePerson()`
This commit is contained in:
committed by
Randell Dawson
parent
e2a00798f8
commit
a16cff7671
@@ -1,10 +1,65 @@
|
||||
---
|
||||
title: Use model.findOne() to Return a Single Matching Document from Your Database
|
||||
---
|
||||
## Use model.findOne() to Return a Single Matching Document from Your Database
|
||||
# Use model.findOne() to Return a Single Matching Document from Your Database
|
||||
|
||||
This is a stub. <a href='https://github.com/freecodecamp/guides/tree/master/src/pages/certifications/apis-and-microservices/mongodb-and-mongoose/use-model.findone-to-return-a-single-matching-document-from-your-database/index.md' target='_blank' rel='nofollow'>Help our community expand it</a>.
|
||||
## Solutions
|
||||
|
||||
<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>.
|
||||
<details><summary>Solution #1 (Click to Show/Hide)</summary>
|
||||
|
||||
<!-- The article goes here, in GitHub-flavored Markdown. Feel free to add YouTube videos, images, and CodePen/JSBin embeds -->
|
||||
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);
|
||||
});
|
||||
};
|
||||
|
||||
/** 5) Use `Model.find()` */
|
||||
var findPeopleByName = function(personName, done) {
|
||||
Person.find({name: personName}, function (err, personFound) {
|
||||
if (err) return console.log(err);
|
||||
done(null, personFound);
|
||||
});
|
||||
};
|
||||
|
||||
/** 6) Use `Model.findOne()` */
|
||||
var findOneByFood = function(food, done) {
|
||||
Person.findOne({favoriteFoods: food}, function (err, data) {
|
||||
if (err) return console.log(err);
|
||||
done(null, data);
|
||||
});
|
||||
};
|
||||
```
|
||||
</details>
|
||||
|
||||
Reference in New Issue
Block a user