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

Have a Database Problem? Speak with an Expert for Free
Get Started >>

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

1
mongod

to start the MongoDB daemon. Then you can run:

1
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

1
2
db.collection.insertOne()
db.collection.insertMany()

Here’s a simple example of an insertOne()

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

Or you can do an insertMany like this:

1
2
3
4
5
6
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.

1
await db.collection('products').insertOne({ name: "Soda", quantity: 100, price: 5.5 });
1
2
3
4
5
6
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:

1
db.collection.find()

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

1
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:

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

With the Node.Js Driver

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

Common Update Commands in MongoDB Shell

Below are the three most common update commands:

1
2
3
db.collection.updateOne()
db.collection.updateMany()
db.collection.replaceOne()

Let’s see an example of each:

1
2
3
4
5
6
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.

1
2
3
4
5
6
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:

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

This would replace the entire document.

With the Node.Js Drivers

1
2
3
4
5
6
await db.collection('products').updateOne(
  { name: 'Bread' },
  {
    $set: { price: 2.55 }
  }
);
1
2
3
4
5
6
await db.collection('products').updateMany(
  { price: { $gt: 1.00 },
  {
    $set: { quantity: 999 }
  }
);
1
2
3
4
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:

1
2
Collection.deleteMany()
Collection.deleteOne()

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

1
db.products.deleteMany({})

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

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

With the Node.Js Drivers

1
await db.collection('inventory').deleteMany({});
1
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!

Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.

Get Started

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.