Insert Documents in MongoDB

CRUD operation is one of the essential concepts of a database system. Inserting data in the database comes under one of the CRUD operations. If you do not insert data in your database, you will not be able to continue with other activities within your document. In this chapter, you will learn about the different concepts and methods that are related to the insert operation in MongoDB.

Various Insert Operations of MongoDB

The insert operation is one of the crucial operations in the database system. MongoDB supports the below mentioned three methods to insert document data in your database:
  1. insert()
  2. insertOne()
  3. insertMany()

The insert() Method

The insert() method is used to insert one or multiple documents in a collection. The collection name is associated with the insert() method and the parameters. The syntax to insert a single document is shown below:

Syntax:
db.collection_Name.insert(JSON document)

In the above syntax, the document will consist of { name: "data_value" }. As it is a JSON document, these documents will consist of the data as name-value pairs, surrounded by curly braces, i.e. {}.

Syntax:
db.movie.insert({"name":"Avengers: Endgame"})
db.movie.find()
Output:


The _id which is provided by MongoDB is a 12-byte value of ObjectId which is prepared from the following values:

  • a 4-byte value denoting the seconds as Unix epoch,
  • a 3-byte device identifier value,
  • a 2-byte processing id,
  • a 3 byte counter which is a random value.

Create Multiple Documents Using insert() Method

It is also possible for you to insert multiple document values in a particular insert() method.

Let us take an example where you can insert multiple documents at a time:

Example:
db.movie.insert(
[
  { name: "Avengers: Infinity War" },
  { name: "Avengers: Endgame" }
]
)

It is to be noted that the documents are supplied in the form of an array. Document values are packed or enclosed in square brackets [] and separated by commas.

Executing the above statements will pop up with messages something like this:

Output:

Embedded Documents

MongoDB also allow users to create document containing other documents, arrays of values, as well as arrays of documents.

Example:
db.writer.insert({
  writername: "Stan Lee",
  comics: [
    { comics: "DC Comics", year: 2004, name: "Superman" },
    { project: "DC Comics", year: 2001, level: "Batman" },
    { project: "Marvel Comics", year: 1968, level: "Captain America" }
  ]
})
Output:

The insertOne() Method

Another way to insert documents is by using the insertOne() method for a single document in a collection:

Example:
db.movie.insertOne({ _id: 2, writername: "Stan Lee", name: "Aquaman" })

In this case, you have a particular non-existent collection of data. In the case of the insert() method, a precise collection will get produced in case it does not exist previously.

Here you will observe that the output appeared to be different in format than that of insert() method:

Output:

insertMany() Method

As the name is explaining its working, is used for inserting multiple documents:

Example:
db.developers.insertMany(
[
  { _id: 20, devname: "John Wick", tools: "Visual Studio", born: 1948 },
  { _id: 21, devname: "Ganesh Roy", tools: "Net Beans", born: 1945 },
  { _id: 22, devname: "Deeksha Raul", tools: "Unity 3D", born: 1954 }
]
)
Output:


Scroll Back to Top