CRUD operations in MongoDB - A summary of the most common methods

Introduction

In this article we’ll discuss some of the most common CRUD operations in MongoDB. There are many options and we’ll highlight the ones that you’ll be using most frequently. We’ll show you the basic commands in the MongoDB Shell first because the function names are for the most part the same across the different drivers. We’ll also show a few examples in the Node.Js drivers so you can see how you might implement them in a specific driver.

Prerequisites

If you’d like to follow along you should have MongoDB installed. You can get into the MongoDB Shell by first running

mongod

to start the MongoDB daemon. Then you can run:

mongo

to get into the MongoDB shell.

CRUD

CRUD stands for Create, Read, Update, and Delete. These are the types of operations you’ll use in a database technology.

Common Create Commands in MongoDB Shell

For creating documents the two most common methods you’ll use are

db.collection.insertOne()
db.collection.insertMany()

Here’s a simple example of an insertOne()

db.products.insertOne(
   { name: "Soda", quantity: 100, price: 5.5 }
)

Or you can do an insertMany like this:

db.products.insertMany(
[
{ name: "Bread", quantity: 9, price: 2.5 },
{ name: "Milk", quantity: 4, price: 3.5 }
]
)

With the Node.Js Driver

Note await is because it’s an asynchronous function.

await db.collection('products').insertOne({ name: "Soda", quantity: 100, price: 5.5 });
await db.collection('products').insertMany(
[
{ name: "Bread", quantity: 9, price: 2.5 },
{ name: "Milk", quantity: 4, price: 3.5 }
]
)

Common Read Commands in MongoDB Shell

For reading(“querying”) documents you’ll be utilizing the find() command:

db.collection.find()

The most basic example of using this is querying all the documents in a collection like this:

db.inventory.find( {} )

The empty document as the parameter specifies that you have no limiting conditions.

Now let’s query for documents that have a certain condition like this:

db.inventory.find( { name: "Bread"} )

With the Node.Js Driver

const cursor = db.collection('products').find({});
const cursor = db.collection('inventory').find({ name: "Bread" });

Common Update Commands in MongoDB Shell

Below are the three most common update commands:

db.collection.updateOne()
db.collection.updateMany()
db.collection.replaceOne()

Let’s see an example of each:

db.products.updateOne(
   { name: "Bread" },
   {
     $set: { price: 2.99 }
   }
)

This will only update the first document that matches the criteria where name = “Bread”. If there was more than one document with name = “Bread” it would only update the first one.

db.products.updateMany(
   { price: { $gt: 1.00 } },
   {
     $set: { quantity: 999 }
   }
)

This would update any document with a price greater than a dollar to have a quantity of 999.

If you want to replace an entire document you’d use a command similar to this:

db.inventory.replaceOne(
   { name: "Bread" },
   { name: "Wheat Bread", quantity: 500, price: 1.25 }
)

This would replace the entire document.

With the Node.Js Drivers

await db.collection('products').updateOne(
  { name: 'Bread' },
  {
    $set: { price: 2.55 }
  }
);
await db.collection('products').updateMany(
  { price: { $gt: 1.00 },
  {
    $set: { quantity: 999 }
  }
);
db.collection('products').replaceOne(
   { name: "Bread" },
   { name: "Wheat Bread", quantity: 500, price: 1.25 }
)

Common Delete Commands in MongoDB Shell

The two most common delete commands:

Collection.deleteMany()
Collection.deleteOne()

Let’s see an example of each. The following command will delete all the documents in a collection:

db.products.deleteMany({})

Now let’s try deleting a document that has the name “Soda”:

db.products.deleteOne( { name: "Soda" } )

With the Node.Js Drivers

await db.collection('inventory').deleteMany({});
await db.collection('inventory').deleteOne({ name: "Soda" });

Conclusion

We hope you’ve enjoyed this review of the most common CRUD commands in MongoDB. We showed demos of how to do them in both the MongoDB Shell and using the official Node.Js drivers. The syntax with your driver should mimic closely what you see here.

If you need someone to help you manage the complexities of your database please don’t hesitate to reach out to us at Object Rocket.

Pilot the ObjectRocket platform free for 30 Days

It's easy to get started. Imagine the time you'll save by not worrying about database management. Let's do this!

PILOT FREE FOR 30 DAYS

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.