2018-10-25 20:29:56 +02:00
---
id: 587d7fb8367417b2b2512c11
title: Delete Many Documents with model.remove()
challengeType: 2
2019-08-05 09:17:33 -07:00
forumTopicId: 301538
2021-01-13 03:31:00 +01:00
dashedName: delete-many-documents-with-model-remove
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
`Model.remove()` is useful to delete all the documents matching given criteria.
2020-11-27 19:02:05 +01:00
# --instructions--
2020-11-03 14:22:55 +00:00
Modify the `removeManyPeople` function to delete all the people whose name is within the variable `nameToRemove` , using `Model.remove()` . Pass it to a query document with the `name` field set, and a callback.
**Note:** The `Model.remove()` doesn’ t return the deleted document, but a JSON object containing the outcome of the operation, and the number of items affected. Don’ t forget to pass it to the `done()` callback, since we use it in tests.
2020-11-27 19:02:05 +01:00
# --hints--
2018-10-25 20:29:56 +02:00
2020-11-27 19:02:05 +01:00
Deleting many items at once should succeed
2020-11-03 14:22:55 +00:00
2020-11-27 19:02:05 +01:00
```js
(getUserInput) =>
$.ajax({
url: getUserInput('url') + '/_api/remove-many-people',
type: 'POST',
contentType: 'application/json',
data: JSON.stringify([
{ name: 'Mary', age: 16, favoriteFoods: ['lollipop'] },
{ name: 'Mary', age: 21, favoriteFoods: ['steak'] }
])
}).then(
(data) => {
assert.isTrue(!!data.ok, 'The mongo stats are not what expected');
assert.equal(
data.n,
2,
'The number of items affected is not what expected'
);
assert.equal(data.count, 0, 'the db items count is not what expected');
},
(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
```