Back to Freecodecamp

Use model.findById() to Search Your Database By _id

curriculum/challenges/english/blocks/mongodb-and-mongoose/587d7fb7367417b2b2512c0d.md

latest956 B
Original Source

--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.

--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.

--hints--

Find an item by Id should succeed

js
  const response = await fetch(code + '/_api/find-by-id');
  if (!response.ok) {
    throw new Error(await response.text());
  }
  const data = await response.json();
  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');