Section 25 – Lessons 343 to 346 – Complete 2020 Web Development Bootcamp

Lesson 343 – MongoDB CRUD Operations in the Shell: Reading and Queries

To find all the data in a collection you can use the .find() method. So to find all of the data in our products collection, you type the following code in the Mongo shell –

db.products.find()

This will return all of the products in the collection –

{ "_id" : 1, "name" : "Pen", "price" : 1.2 }
{ "_id" : 2, "name" : "Pencil", "price" : 0.8 }

If you want to make a query for a specific piece of data in the collection, then you can add a query inside the brackets of the .find() method using query operators, like this –

db.products.find({name: "Pencil"})

This will return all products with a name of “Pencil” –

{ "_id" : 2, "name" : "Pencil", "price" : 0.8 }

If you wanted to find just the products which have a price above $1.00, you could use this query –

db.products.find({price: {$gt: 1}})

This will return –

{ "_id" : 1, "name" : "Pen", "price" : 1.2 }

This page contains a list of all of the various query selectors that you can use to query a collection.

The .find() method also takes a second parameter which is called – projection. To use a projection in a search of the products category, you can use –

db.products.find({_id: 1}, {name: 1})

The projection is “{name: 1}” and the “1” in that projection means “true” so it will only return something if there is a name.

This code returns –

{ "_id" : 1, "name" : "Pen" }

The projection in the .find() method is a way to specify which field in the data you want returned.

This page contains a list of the various projection operators that you can use .

Lesson 344 – MongoDB CRUD Operations in the Shell: Update

To update data in a collection, you can use the .update() method.

If you are updating just one item in the collection you can use the .updateOne() method. So to update the Pen item in the project collection by adding a stock item, you can use this code –

db.products.updateOne({_id: 1}, {$set: {stock: 32}})

To check that the Pen item now has a stock filed, you can run –

db.products.find()

This should be returned –

{ "_id" : 1, "name" : "Pen", "price" : 1.2, "stock" : 32 }
{ "_id" : 2, "name" : "Pencil", "price" : 0.8 }

The challenge in this lesson is to update the Pencil item to have a stock value of 12. To do this, you use this code –

db.products.updateOne({_id: 2}, {$set: {stock: 12}})

Again, you can confirm that the pencil item has been updated by using the .find() method –

db.products.find()

This should be returned –

{ "_id" : 1, "name" : "Pen", "price" : 1.2, "stock" : 32 }
{ "_id" : 2, "name" : "Pencil", "price" : 0.8, "stock" : 12 }

Lesson 345 – MongoDB CRUD Operations in the Shell: Delete

The two methods for deleting data from a Mongo database are .deleteOne() and .deleteMany().

The challenge is to remove the Pencil record by reading and using the documentation on MongoDB.

You can complete this challenge using the .deleteOne() method. This is the code –

db.products.deleteOne({_id: 2})

That should give this acknowledgment –

{ "acknowledged" : true, "deletedCount" : 1 }

To confirm that the Pencil record has been delete, run –

db.products.find()

If the pencil item has been deleted, that will return just the Pen record –

{ "_id" : 1, "name" : "Pen", "price" : 1.2, "stock" : 32 }

Lesson 346 – Relationships in MongoDB

The first step in this lesson is to create a new item in the products collection. To do this, you can use this code –

db.products.insert({_id: 3, name: "Eraser", price: 1.30, stock: 43, re
views: [{authorName: "Sally", rating: 5, review: "Best eraser ever!"}]})

The challenge in this lesson is to re-create the Pencil record which was deleted in the previous lesson and to make up two reviews to embed in the Pencil record.

This is the code to complete this challenge –

db.products.insertOne({_id: 2, name: "Pencil", price: 0.80, stock: 12, reviews: [{authorName: "Jim", rating: 5, review: "Great pencil!"}, {authorName: "Jane", rating: 2, review: "Not great!"}]})

If the Pencil record was inserted, you will get this message –

{ "acknowledged" : true, "insertedId" : 2 }