2018-10-25 20:29:56 +02:00
---
id: 587d7fb7367417b2b2512c0d
title: Use model.findById() to Search Your Database By _id
challengeType: 2
2019-08-05 09:17:33 -07:00
forumTopicId: 301544
2018-10-25 20:29:56 +02:00
---
## Description
2020-11-03 14:22:55 +00:00
2018-10-25 20:29:56 +02:00
< section id = 'description' >
2020-11-03 14:22:55 +00:00
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.
2018-10-25 20:29:56 +02:00
< / section >
## Instructions
2020-11-03 14:22:55 +00:00
2018-10-25 20:29:56 +02:00
< section id = 'instructions' >
2020-11-03 14:22:55 +00:00
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.
2018-10-25 20:29:56 +02:00
< / section >
## Tests
2020-11-03 14:22:55 +00:00
2018-10-25 20:29:56 +02:00
< section id = 'tests' >
```yml
2018-10-27 12:53:05 +03:00
tests:
- text: Find an item by Id should succeed
2020-11-03 14:22:55 +00:00
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); })
2018-10-25 20:29:56 +02:00
```
< / section >
## Challenge Seed
2020-11-03 14:22:55 +00:00
2018-10-25 20:29:56 +02:00
< section id = 'challengeSeed' >
< / section >
## Solution
2020-11-03 14:22:55 +00:00
2018-10-25 20:29:56 +02:00
< section id = 'solution' >
```js
2019-10-24 10:08:13 +05:30
/**
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.
*/
2018-10-25 20:29:56 +02:00
```
2019-02-27 14:41:19 -05:00
2018-10-25 20:29:56 +02:00
< / section >