Ch 2 · MongoDB CRUD Operations · MongoDB

Topic 2 of 3 in MongoDB — Complete Tutorial (Beginner) — 8 lessons.

Insert and Find Operations

MongoDB's four basic operations are often called CRUD: Create, Read, Update and Delete. This card covers the first two, creating and reading. You will add documents with insertOne and insertMany, fetch one match with findOne, and get many with find, which returns a cursor you turn into an array with toArray. Filters such as $gt and $lt narrow down the results. Updating and deleting come next, in Card 2.5.

Insert and Find Operations in Code

The code inserts three blog posts, one with insertOne and two with insertMany. Then it reads them back three ways: findOne fetches a post by title, find with $gt returns every post with more than 10 likes, and countDocuments with $gte counts posts on 10 likes or more. Note the .toArray() call needed after find.

Practice: Insert and Find Operations

Practise inserting and querying a small product catalogue. You will add five products, each with a name, price and category, then find the ones costing more than 50 and count how many belong to one category. A correct answer shows you can write a filter object, use a comparison operator, and remember that find returns a cursor rather than an array.

Solution & Challenge: Insert and Find Operations

Compare your answer with the worked solution, which inserts five products in one call, finds those over 50 and counts the electronics. The challenge builds a fuller inventory: at least ten products, searches that combine a price range and a category with $and, sorting by price to find the most expensive item, and a projection that returns only names and prices.

Update and Delete Operations

updateOne, updateMany, deleteOne, and deleteMany modify or remove documents. Each takes a filter first to choose which documents are affected. For updates, a second argument uses operators such as $set to change a field, $inc to add to a number, $unset to remove a field and $push to append to an array. Take care with delete filters: an empty filter matches every document.

Update and Delete Operations in Code

This example carries on with the three posts from Card 2.2 and changes them one at a time and in bulk. See how a single updateOne call combines $set and $inc, and how updateMany labels every post with fewer than 10 likes, which is only Post 3. Then deleteOne removes Post 2 by title and deleteMany clears the labelled post. Check deletedCount in each result, which tells you how many documents actually went.

Practice: Update and Delete Operations

Now practise changing and removing product documents. You will update one product's price, increase another product's stock level, and delete every product that has run out. A correct answer uses update operators rather than replacing whole documents, and a filter that matches only the out-of-stock products, so nothing else is removed by accident.

Solution & Challenge: Update and Delete Operations

Check your work against the worked solution, which lowers the laptop's price with $set, adds stock with $inc and removes items with zero or less in stock. The challenge is a shopping cart: add items with $push, change quantities using $set with dot notation, remove items with $pull, clear the cart with deleteOne, and cope with an empty cart.

All topics in MongoDB Beginner

  1. MongoDB Atlas and Cloud Deployment
  2. MongoDB CRUD Operations
  3. MongoDB Introduction and Setup