freeCodeCamp/curriculum/challenges/chinese/05-apis-and-microservices/mongodb-and-mongoose/use-model.findbyid-to-searc...

1.3 KiB
Raw Blame History

id title localeTitle challengeType
587d7fb7367417b2b2512c0d Use model.findById() to Search Your Database By _id 使用model.findById按_id搜索数据库 2

Description

0保存文档时mongodb会自动添加字段_id并将其设置为唯一的字母数字键。按_id搜索是一种非常频繁的操作因此mongoose为它提供了一种专用方法。使用Model.findById - > Person找到具有给定_id的仅!!人物。使用函数参数personId作为搜索关键字。

Instructions

Tests

tests:
  - text: 找到Id应该成功的项目
    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

// solution required