Computer scienceFundamentalsSQL and DatabasesDBMSMongoDBOperations and operatorsCRUD operations

Create Operations

4 minutes read

Alright, you already knew how to create a database in MongoDB. Now we need to fill it with some data. In this topic you will learn the create operations in MongoDB that will help you add some data to your database collection.

Inserting data using MongoDB Atlas

There exist two methods to insert data in MongoDB: using MongoDB Atlas or with the CLI (Command Line Interface). Let's take a look at the first option. You should already have an account and a database with a collection. So, go ahead!

First of all, you need to go the Collections tab and choose your collection. In our case, the collection is called the same as the database, books.

Collections on mongodb

As you can see, there is no data yet, so let's fix it. To create a new record in the collection, click the INSERT DOCUMENT button. You will see this window:

Insert to collection

MongoDB Atlas already created a unique _id for your new record, so you don't need to worry about it. On the left side, there will be the names of fields and the data itself, on the right side — their datatypes.

Let's create a record for the new book. It will have such fields as title, author, and year. We will add some information about George Orwell's "1984" novel:

Data collection

Great! Now we can click the Insert button and the data will be recorded in our collection. As you can see, our book has been successfully added to the collection books:

Books collection

So, we have created a new record in our collection using MongoDB Atlas. Now let's talk about the other method.

Inserting data via CLI

Let's now look at the method of adding data using MongoDB command-line interface. You should already have a database and collection as well. In our case it will also be called books.

There are two functions that are used to create a record in the collection:

  • db.collection.insertOne() to add just one record
  • db.collection.insertMany() to add multiple records.

Instead of collection there should be the name of your collection. In the brackets you need to write your data in JSON format (field: value). Take a look at the example below for the same book as in the previous paragraph:

db.books.insertOne(
  {
    title: "1984",
    author: "G. Orwell",
    year: 1949
  }
)

Note that you still don't need to paste _id field by yourself, MongoDB will generate it as well. But if you want to generate your own ID, you can do this manually by adding the _id field.

So, let's add multiple entries at the same time using the insertMany() method:

db.books.insertMany([
  {
    title: "The Adventures of Sherlock Holmes",
    author: "C. Doyle",
    year: 1892
  },
  {
    title: "Fahrenheit 451",
    author: "R. Bradbury",
    year: 1953 
  },
  {
    title: "Three Comrades",
    author: "E. Remarque",
    year: 1936
  }
])

As you can see, the insertMany() method is very useful when you need to add several records at once.

Conclusion

In this topic you've learned about the create operations in MongoDB. Here are a few highlights from the topic:

  • Data can be inserted using MongoDB Atlas or using MongoDB command-line interface.
  • Use db.collection.insertOne() to add a single record.
  • Use db.collection.insertMany() to add several records.
  • Data is inserted in JSON format.

Now, we hope you are ready for some practice! Go ahead!

17 learners liked this piece of theory. 0 didn't like it. What about you?
Report a typo