MongoDB Tutorial

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

Introduction

MongoDB is one of the most popular NoSQL databases. It stores data in BSON format, which is nothing but Binary-encoded JSON. A record is stored in a document, and such documents are stored in a collection. The collections are stored in a database. MongoDB provides several methods that could be used to perform CRUD and other operations. We hope you gain some knowledge from this quick MongoDB tutorial.

Let’s begin with running the mongo shell. Open a terminal and go to the following directory.

1
C:\Program Files\MongoDB\Server\4.2\bin

Note: 4.2 is the version installed in my machine. It can vary.

Run the “mongod” command.

Image from Gyazo

Now, we can run the mongo shell. Open another terminal but do not close the terminal where mongod command is running. In the new terminal, type the ‘mongo’ command.

Image from Gyazo

Creating a database and collection

So the very first step is to create a database. It is very simple. Just write the name of the database ahead of the “use” command.

Image from Gyazo

Now, we are switched to the petstore database. A database can contain any number of collections. There are two ways of creating a collection. We will use the createCollection() method.

Image from Gyazo

The name of the collection here is “dogs”. We have a database and a collection in it.

Inserting documents

Let’s insert documents in the dogs collection. There are two methods for inserting documents in a collection – insert() and insertMany().

insert()

The insert() method inserts a single document in a collection. Observe the following document.

1
2
3
4
5
6
{
    "name" : "Romeo",
    "age" : 4,
    "breed" : "Labrador"

}

Let’s insert this document in the dogs collection using the insert() method.

Image from Gyazo

insertMany()

As the name suggests, the insertMany() method inserts multiple documents in a collection. It takes an array of documents as an argument.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[
  {
    name: "Maxi",
    age: 2,
    breed: "German Shephard"
  },
  {
    name: "Scooby",
    age: 3,
    breed: "Great Dane"
  },
  {
    name: "Ronny",
    age: 1,
    breed: "Labrador"
  }
];

Let’s insert these documents in the dogs collection using the insertMany() method.

Image from Gyazo

So we inserted a total of four documents in the collection. Next, we will see how to read these documents.

Reading documents

There are few methods to read data in MongoDB. The primary method is the find() method. It is necessary to provide the first argument to the find() method. This argument is the query that contains the condition for matching documents. If we want all the documents to be fetched, we just provide an empty object as a query.

Image from Gyazo

But if we want specific documents, we can provide a query. Suppose, we want all those documents where the value of the breed field is “Labrador”. We can simply use the following query.

1
{ "breed" : "Labrador" }

Image from Gyazo

Deleting documents

Let’s delete documents from the collection.

deleteOne()

To delete a single document, we can use the deleteOne() method. This method will match a single document according to the query and then deletes it.

Image from Gyazo

It deletes the first document where the value of the name field is “Ronny”.

deleteMany()

Now, the deleteMany method deletes multiple documents according to the query. But we can also the deleteMany() method to delete all the documents in a collection. All we have to do is simply provide an empty object as a query.

Image from Gyazo

Let’s use the find() method again to see if there is any document left in the petstore collection or not.

Image from Gyazo

All the documents are deleted!

Conclusion

This was a simple MongoDB tutorial. There is a lot more in MongoDB. There are several methods and operators that could be used according to the requirements. Moreover, MongoDB can be used with Nodejs with the help of mongoose ODM.

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.