Files
freeCodeCamp/curriculum/challenges/english/05-apis-and-microservices/mongodb-and-mongoose/use-model.findbyid-to-search-your-database-by-id.english.md
Ambar Mutha bcef08424c add comment explaining back-end challenges don't need a solution (#37111)
* add solution to a challenge

add solution to the challenge: add-a-description-to-your-package-json

* add comment to all backend challenges
The comment expllains that these challenges don't need a solution.
2019-10-24 13:38:13 +09:00

1.5 KiB

id, title, challengeType, forumTopicId
id title challengeType forumTopicId
587d7fb7367417b2b2512c0d Use model.findById() to Search Your Database By _id 2 301544

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

Find the (only!!) person having a given _id, using Model.findById() -> Person. Use the function argument personId as the search key.

Tests

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); })"

Challenge Seed

Solution

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