freeCodeCamp/curriculum/challenges/english/05-apis-and-microservices/mongodb-and-mongoose/perform-classic-updates-by-...

51 lines
2.4 KiB
Markdown
Raw Normal View History

2018-10-25 18:29:56 +00:00
---
id: 587d7fb8367417b2b2512c0e
title: 'Perform Classic Updates by Running Find, Edit, then Save'
challengeType: 2
forumTopicId: 301541
2018-10-25 18:29:56 +00:00
---
## Description
<section id='description'>
fix(curriculum) Replace <code> with <blockquote>for code sections (#35442) * fix: reformatted code and moved instructions * fix: replaced code with blockquotes * Update curriculum/challenges/english/05-apis-and-microservices/mongodb-and-mongoose/chain-search-query-helpers-to-narrow-search-results.english.md Co-Authored-By: RandellDawson <5313213+RandellDawson@users.noreply.github.com> * Update curriculum/challenges/english/05-apis-and-microservices/mongodb-and-mongoose/install-and-set-up-mongoose.english.md Co-Authored-By: RandellDawson <5313213+RandellDawson@users.noreply.github.com> * Update curriculum/challenges/english/05-apis-and-microservices/mongodb-and-mongoose/create-a-model.english.md Co-Authored-By: RandellDawson <5313213+RandellDawson@users.noreply.github.com> * fix: made a few recommended changes of text * fix: moved </blockquote> to new line Co-Authored-By: RandellDawson <5313213+RandellDawson@users.noreply.github.com> * fix: removed extra space Co-Authored-By: RandellDawson <5313213+RandellDawson@users.noreply.github.com> * Update curriculum/challenges/english/05-apis-and-microservices/mongodb-and-mongoose/chain-search-query-helpers-to-narrow-search-results.english.md Co-Authored-By: RandellDawson <5313213+RandellDawson@users.noreply.github.com> * Update curriculum/challenges/english/05-apis-and-microservices/mongodb-and-mongoose/create-a-model.english.md Co-Authored-By: RandellDawson <5313213+RandellDawson@users.noreply.github.com> * fix: added the word The before code tag section
2019-03-09 14:49:19 +00:00
In the good old days this was what you needed to do if you wanted to edit a document and be able to use it somehow e.g. sending it back in a server response. Mongoose has a dedicated updating method : <code>Model.update()</code>. It is bound to the low-level mongo driver. It can bulk edit many documents matching certain criteria, but it doesnt send back the updated document, only a status message. Furthermore it makes model validations difficult, because it just directly calls the mongo driver.
2018-10-25 18:29:56 +00:00
</section>
## Instructions
<section id='instructions'>
fix(curriculum) Replace <code> with <blockquote>for code sections (#35442) * fix: reformatted code and moved instructions * fix: replaced code with blockquotes * Update curriculum/challenges/english/05-apis-and-microservices/mongodb-and-mongoose/chain-search-query-helpers-to-narrow-search-results.english.md Co-Authored-By: RandellDawson <5313213+RandellDawson@users.noreply.github.com> * Update curriculum/challenges/english/05-apis-and-microservices/mongodb-and-mongoose/install-and-set-up-mongoose.english.md Co-Authored-By: RandellDawson <5313213+RandellDawson@users.noreply.github.com> * Update curriculum/challenges/english/05-apis-and-microservices/mongodb-and-mongoose/create-a-model.english.md Co-Authored-By: RandellDawson <5313213+RandellDawson@users.noreply.github.com> * fix: made a few recommended changes of text * fix: moved </blockquote> to new line Co-Authored-By: RandellDawson <5313213+RandellDawson@users.noreply.github.com> * fix: removed extra space Co-Authored-By: RandellDawson <5313213+RandellDawson@users.noreply.github.com> * Update curriculum/challenges/english/05-apis-and-microservices/mongodb-and-mongoose/chain-search-query-helpers-to-narrow-search-results.english.md Co-Authored-By: RandellDawson <5313213+RandellDawson@users.noreply.github.com> * Update curriculum/challenges/english/05-apis-and-microservices/mongodb-and-mongoose/create-a-model.english.md Co-Authored-By: RandellDawson <5313213+RandellDawson@users.noreply.github.com> * fix: added the word The before code tag section
2019-03-09 14:49:19 +00:00
Find a person by <code>_id</code> ( use any of the above methods ) with the parameter <code>personId</code> as search key. Add &quot;hamburger&quot; to the list of the person&apos;s <code>favoriteFoods</code> (you can use <code>Array.push()</code>). Then - inside the find callback - <code>save()</code> the updated <code>Person</code>.
<strong>Note:</strong> This may be tricky, if in your Schema, you declared <code>favoriteFoods</code> as an Array, without specifying the type (i.e. <code>[String]</code>). In that case, <code>favoriteFoods</code> defaults to Mixed type, and you have to manually mark it as edited using <code>document.markModified('edited-field')</code>. See [Mongoose documentation](https://mongoosejs.com/docs/schematypes.html#Mixed)
2018-10-25 18:29:56 +00:00
</section>
## Tests
<section id='tests'>
```yml
tests:
- text: Find-edit-update an item should succeed
testString: "getUserInput => $.post(getUserInput('url') + '/_api/find-edit-save', {name:'Poldo', age: 40, favoriteFoods:['spaghetti']}).then(data => { assert.equal(data.name, 'Poldo', 'item.name is not what is expected'); assert.equal(data.age, 40, 'item.age is not what expected'); assert.deepEqual(data.favoriteFoods, ['spaghetti', 'hamburger'], 'item.favoriteFoods is not what expected'); assert.equal(data.__v, 1, 'The item should be previously edited'); }, xhr => { throw new Error(xhr.responseText); })"
2018-10-25 18:29:56 +00:00
```
</section>
## Challenge Seed
<section id='challengeSeed'>
</section>
## Solution
<section id='solution'>
```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.
*/
2018-10-25 18:29:56 +00:00
```
2018-10-25 18:29:56 +00:00
</section>