2019-05-06 11:31:26 +00:00
|
|
|
---
|
|
|
|
id: 587d7fb8367417b2b2512c10
|
2021-02-16 21:01:20 +00:00
|
|
|
title: 使用 model.findByIdAndRemove 删除一个 document
|
2019-05-06 11:31:26 +00:00
|
|
|
challengeType: 2
|
2020-09-17 10:53:54 +00:00
|
|
|
forumTopicId: 301539
|
2021-01-13 02:31:00 +00:00
|
|
|
dashedName: delete-one-document-using-model-findbyidandremove
|
2019-05-06 11:31:26 +00:00
|
|
|
---
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
# --description--
|
2019-05-06 11:31:26 +00:00
|
|
|
|
2021-02-16 21:01:20 +00:00
|
|
|
`findByIdAndRemove` 和 `findOneAndRemove` 类似于我们之前的更新方法, 它们将被删除的 document 传给数据库。 和之前一样,使用函数参数 `personId` 作为查询关键字。
|
2021-02-06 04:42:36 +00:00
|
|
|
|
|
|
|
# --instructions--
|
|
|
|
|
2021-02-16 21:01:20 +00:00
|
|
|
修改 `removeById` 函数,通过 `_id` 删除一个人的数据, 可以使用 `findByIdAndRemove()` 或 `findOneAndRemove()` 方法。
|
2019-05-06 11:31:26 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
# --hints--
|
2019-05-06 11:31:26 +00:00
|
|
|
|
2021-02-16 21:01:20 +00:00
|
|
|
应当成功地删除一条数据
|
2019-05-06 11:31:26 +00:00
|
|
|
|
|
|
|
```js
|
2020-12-16 07:37:30 +00:00
|
|
|
(getUserInput) =>
|
|
|
|
$.post(getUserInput('url') + '/_api/remove-one-person', {
|
|
|
|
name: 'Jason Bourne',
|
|
|
|
age: 36,
|
|
|
|
favoriteFoods: ['apples']
|
|
|
|
}).then(
|
|
|
|
(data) => {
|
|
|
|
assert.equal(data.name, 'Jason Bourne', 'item.name is not what expected');
|
|
|
|
assert.equal(data.age, 36, 'item.age is not what expected');
|
|
|
|
assert.deepEqual(
|
|
|
|
data.favoriteFoods,
|
|
|
|
['apples'],
|
|
|
|
'item.favoriteFoods is not what expected'
|
|
|
|
);
|
|
|
|
assert.equal(data.__v, 0);
|
|
|
|
assert.equal(data.count, 0, 'the db items count is not what expected');
|
|
|
|
},
|
|
|
|
(xhr) => {
|
|
|
|
throw new Error(xhr.responseText);
|
|
|
|
}
|
|
|
|
);
|
2019-05-06 11:31:26 +00:00
|
|
|
```
|
2020-08-13 15:24:35 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
# --solutions--
|
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
```js
|
|
|
|
/**
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
```
|