FAQ: CRUD II: Inserting and Updating - Modifying Documents

This community-built FAQ covers the “Modifying Documents” exercise from the lesson “CRUD II: Inserting and Updating”.

Paths and Courses
This exercise can be found in the following Codecademy content:

[Beta] Learn MongoDB

FAQs on the exercise Modifying Documents

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in #get-help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

Looking for motivation to keep learning? Join our wider discussions in #community

Learn more about how to use this guide.

Found a bug? Report it online, or post in #community:Codecademy-Bug-Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

It’s quite rare for Codecademy, but i found out a mistake in exercise 9/10 in Crud II lesson. You lost parameter $set in findAndModify method, so it output in reality don’t matches at all with the example version.

1 Like

Very good catch. Without the $set parameter, the existing document is replaced instead of just the specific fields being updated.

Why the inconsistency in parameters between different update methods?

Example1: db.collection.updateOne({queryField: “queryFilter”}, {$set: {fieldToUpdate: “updatedInfo”}}, options);

Example2: db.collection.findAndModify({query: {queryField: “queryFilter”}, update: {fieldToUpdate: “updatedInfo”}, options});

Why the sudden addition of query and update fields?
Why the omission of $set method?

Every language has its own syntax, so it shouldn’t be a surprise that MongoDB Query Language has its own rules and syntax. For any language, documentation is important in understanding the arguments, returns, optional values, special remarks associated with any method/command.

Mongosh (MongoDB Shell) documentation:

Documentation for updateOne: https://www.mongodb.com/docs/manual/reference/method/db.collection.updateOne/

Documentation for findAndModify: https://www.mongodb.com/docs/manual/reference/method/db.collection.findAndModify/

If you look at the documentation for updateOne, its definition is:

db.collection.updateOne(filter, update, options)

where filter and options are specified to be documents, whereas update can be document or pipeline. options is an optional document. The developers could have embedded these as fields of a document, but in my opinion, they wanted to keep it simple. The first document will be the filter and the second document the update (it is a logical order), so assigning them to fields may make the call to updateOne more verbose or intricate than necessary.

Contrast this with the definition of findAndModify:


At the top level, the parameters are placed as fields in a document.
In updateOne, the parameter documents aren’t embedded in a document i.e. in updateOne the call is:

updateOne( {filter doc}, {update doc}, {options doc} ) // Correct 
updateOne({ {filter doc}, {update doc}, {options doc} }) // Not Correct

In findAndModify within the top-level document, the query, update and other parameters are specified as fields like so:

db.collection.findAndModify({ query: {...}, update: {...}, upsert: <boolean>, 
                             sort: {...}, remove: <boolean>, ... })

Notice in this method, the optional fields aren’t consolidated into one options document. Rather they are separate fields. It makes the method more versatile. For example, we can set the field remove: true and omit the update field altogether (this will remove the document matching the query from the collection). We can also specify sort: {...} to select which matching document gets picked. There is a lot more functionality in the findAndModify method. If the developers chose the same approach for this method as updateOne by not using fields, then we would be constrained to order the arguments in a very specific and limited order. Using the field model allows us to to change the order of the fields while passing arguments.

The above is just my opinion. I am not privy to the thinking and considerations of the developers. The simple answer is: This is how the developers chose to implement the methods in mongosh.

As pointed out by barbaka, the course author(s) made a mistake.

// Suppose we have a document in employees collection,
	"_id" : ObjectId("637cf3dc4a12ed7cca2888b6"),
	"empId" : 1,
	"name" : "Clark",
	"dept" : "Sales",
	"age" : 44

db.employees.findAndModify({query: {name: 'Clark'}, update: {$set: {age:88}}, new: true});
# With $set. The age field is updated. The rest remains same.
	"_id" : ObjectId("637cf3dc4a12ed7cca2888b6"),
	"empId" : 1,
	"name" : "Clark",
	"dept" : "Sales",
	"age" : 88

db.employees.findAndModify({query: {name: 'Clark'}, update: {age:88}, new: true});
# Without $set. Only the _id is the same. Rest of the document fields are gone.

     "_id" : ObjectId("637cf3dc4a12ed7cca2888b6"), 
    "age" : 88 

Hello guys,
i have a question regarding upsert :

  • In the concept 7 (upserting a document) : The created document have both fields in the filter and update arguments (using updateOne() )
  • In this concept , when we used upsert with findAndModify() : The created document only have the fields specified in the modify argument!

Its a diffirent behaviour , so confusing :confused:

1 Like

Well, i have tried using $set in the update argument, The new created document now included the fields specified in the update argument

I noticed that if I hit tab, the method findAndModify is not listed as an available option. Any ideas why?
crud-ii-inserting-and-updating exercises find-and-modify missing-findAndModify-when-hitting-tab-key 5-25-2023