2018-10-25 20:29:56 +02:00
---
id: 587d7fb7367417b2b2512c0d
2021-11-01 07:38:37 -07:00
title: Usa model.findById() para buscar en tu base de datos por _id
2018-10-25 20:29:56 +02:00
challengeType: 2
2019-08-05 09:17:33 -07:00
forumTopicId: 301544
2021-01-13 03:31:00 +01:00
dashedName: use-model-findbyid-to-search-your-database-by-id
2018-10-25 20:29:56 +02:00
---
2020-11-27 19:02:05 +01:00
# --description--
2020-11-03 14:22:55 +00:00
2021-11-01 07:38:37 -07:00
Al guardar un documento, MongoDB añade automáticamente el campo `_id` , y lo establece como una clave alfanumérica única. Buscar por `_id` es una operación extremadamente frecuente, así que Mongoose proporciona un método dedicado para ello.
2020-11-03 14:22:55 +00:00
2020-11-27 19:02:05 +01:00
# --instructions--
2020-11-03 14:22:55 +00:00
2021-11-01 07:38:37 -07:00
Modifique el `findPersonById` para encontrar la única persona que tenga una determinada `_id` , usando `Model.findById() -> Person` . Utiliza el argumento de la función `personId` como clave de búsqueda.
2020-11-03 14:22:55 +00:00
2020-11-27 19:02:05 +01:00
# --hints--
2018-10-25 20:29:56 +02:00
2021-11-01 07:38:37 -07:00
Encontrar un elemento por Id debe ser exitoso
2020-11-03 14:22:55 +00:00
2020-11-27 19:02:05 +01:00
```js
(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
```
2020-11-27 19:02:05 +01:00
# --solutions--
2018-10-25 20:29:56 +02:00
```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
```