FAQ: CRUD II: Deleting Documents - Review

This community-built FAQ covers the “Review” exercise from the lesson “CRUD II: Deleting Documents”.

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

Learn MongoDB

FAQs on the exercise Review

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 Language Help.

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

Need broader help or resources? Head to Language Help and Tips and Resources. If you are wanting feedback or inspiration for a project, check out Projects.

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 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!

Here are my solutions to the “Optional Tasks”.

1.Delete any restaurants that have received a "C" grade via the grades field. After all, we have to keep our standards high!

See documents with a C grade in the grades field:
db.listingsAndReviews.find({"grades.grade": "C"})

Delete all documents with a C grade in the grades field:
db.listingsAndReviews.deleteMany({"grades.grade": "C"})

See result: db.listingsAndReviews.find({"grades.grade": "C"})

2.Replace the cuisine of a restaurant of your choosing.

My guess is that they want us to use the replaceOne() method. So for example like this:

db.listingsAndReviews.replaceOne({ name: "Cafe Bar"},{ name: "Cafe Bar", cuisine: "American" })

This will replace the first found document with the name “Cafe Bar” and replace it with a new document with the fields: “Cafe Bar” and cuisine: “American” })

I found that the updateOne() method is better:
db.listingsAndReviews.updateOne({ "name": "Wakamba"}, { $set : {cuisine: "Mexican"}} )

See result : db.listingsAndReviews.find({ "name": "Wakamba"})

This will not replace the document but just change the cuisine to “American” for the restaurant name “Wakamba”.

3.Choose a restaurant with multiple grades of "A". Replace the document, so it no longer has a grades field but instead has a field named top_restaurant with the value of true.

“Cafe Bar” has several grades of “A”:
db.listingsAndReviews.find({name: "Cafe Bar"})

Again, what they probably want us to do is to use the replaceOne() method:
db.listingsAndReviews.replaceOne( { name: "Cafe Bar" }, { name: "Cafe Bar", top_restaurant: “true” } );

This will replace the document with the name “Cafe Bar” and create a new document with a field called name and the value “Cafe Bar” plus another new field “top_restaurant” and the value “true”.

But for me the updateOne() method would have made sense here as it would not delete all the other fields.

1 Like