Files
freeCodeCamp/curriculum/challenges/english/05-apis-and-microservices/mongodb-and-mongoose/use-model.findbyid-to-search-your-database-by-id.md
Shaun Hamilton e1bae44036 feat(learn): migrate mongodb-mongoose instructions to learn (#39970)
* feat(learn): migrate mongodb-mongoose instructions to learn

* remove extraneous quotation mark

* update with boilerplate change

* grammar changes on instructions

Co-authored-by: Randell Dawson <5313213+RandellDawson@users.noreply.github.com>

Co-authored-by: Randell Dawson <5313213+RandellDawson@users.noreply.github.com>
2020-11-03 08:22:55 -06:00

61 lines
1.5 KiB
Markdown

---
id: 587d7fb7367417b2b2512c0d
title: Use model.findById() to Search Your Database By _id
challengeType: 2
forumTopicId: 301544
---
## Description
<section id='description'>
When saving a document, MongoDB automatically adds the field `_id`, and set it to a unique alphanumeric key. Searching by `_id` is an extremely frequent operation, so Mongoose provides a dedicated method for it.
</section>
## Instructions
<section id='instructions'>
Modify the `findPersonById` to find the only person having a given `_id`, using `Model.findById() -> Person`. Use the function argument `personId` as the search key.
</section>
## Tests
<section id='tests'>
```yml
tests:
- text: Find an item by Id should succeed
testString: |
getUserInput => $.get(getUserInput('url') + '/_api/find-by-id').then(data => {
assert.equal(data.name, 'test', 'item.name is not what expected');
assert.equal(data.age, 0, 'item.age is not what expected');
assert.deepEqual(data.favoriteFoods, ['none'], 'item.favoriteFoods is not what expected');
assert.equal(data.__v, 0, 'The item should be not previously edited');
}, xhr => { throw new Error(xhr.responseText); })
```
</section>
## Challenge Seed
<section id='challengeSeed'>
</section>
## Solution
<section id='solution'>
```js
/**
Backend challenges don't need solutions,
because they would need to be tested against a full working project.
Please check our contributing guidelines to learn more.
*/
```
</section>