MongoDB

MongoDB Insert Multiple Documents

In this article, we will learn how to insert multiple documents at once in a given collection using the MongoDB Shell native commands.

MongoDB insertMany() Method

As the name suggests, the insertMany() method allows us to specify a set of documents and insert them all at once in a given collection.

The following shows the syntax of the insertMany() method:

db.collection.insertMany(
[  , , ... ],
   {
writeConcern: ,
      ordered:
   }
)

The parameter are as follows:

  1. Document – specifies an array of documents to insert into a given collection.
  2. writeConcern – defines the write concern parameters which overrides the default write concern. A write concern is used to define the level of acknowledgement requested from MongoDB for write ops.
  3. Ordered –defines whether the MongoDB instance should execute an ordered or unordered insert.

The method should return an acknowledgment statement which includes a Boolean statement and the number of inserted documents.

Let us take a look at a practical example.

Define a Collection

Let us start by creating a simple collection where we can insert sample data. Login into MongoDB Shell and run the command:

$ mongosh

db.createCollection("employees")

The command above should create a new collection called employees.

Insert Multiple Documents With _id Field

MongoDB is a very flexible database. It allows us to insert documents without specifying the _id field. By default, MongoDB will generate a unique identifier for every document inserted in the collection

The example below shows how to use the insertMany() method to insert multiple documents without the _id field.

db.employees.insertMany([
    { name: "Ryan", department: "Game Development", salary: 120000 },
    { name: "David", department: "Backend Development", salary: 108000 },
    { name: "Rachael", department: "Full Stack Development", salary: 110000 },
    { name: "James", department: "DevOps Development", salary: 110000 },
    { name: "Peter", department: "Game Development", salary: 123000 },
])

If we execute the query above, we should get an output as shown:

{
    "acknowledged" :true,
    "insertedIds" : [
ObjectId("632bd2f28b16030eebd4fad6"),
ObjectId("632bd2f28b16030eebd4fad7"),
ObjectId("632bd2f28b16030eebd4fad8"),
ObjectId("632bd2f28b16030eebd4fad9"),
ObjectId("632bd2f28b16030eebd4fada")
    ]
}

The above shows an acknowledgment message and the automatically generated id values.

We can locate the inserted documents as:

db.employees.find({})

The command above should return the inserted documents as shown:

{
    "_id" : ObjectId("632bd2f28b16030eebd4fad6"),
    "name" : "Ryan",
    "department" : "Game Development",
    "salary" : 120000.0
}
{
    "_id" : ObjectId("632bd2f28b16030eebd4fad7"),
    "name" : "David",
    "department" : "Backend Development",
    "salary" : 108000.0
}
{
    "_id" : ObjectId("632bd2f28b16030eebd4fad8"),
    "name" : "Rachael",
    "department" : "Full Stack Development",
    "salary" : 110000.0
}
{
    "_id" : ObjectId("632bd2f28b16030eebd4fad9"),
    "name" : "James",
    "department" : "DevOps Development",
    "salary" : 110000.0
}

Insert Multiple Documents With _id Field

If we do not wish MongoDB to generated an _id value, we can explicitly specify the values for the _id column as shown in the example below:

db.employees.insertMany([
    { _id: 1, name: "Ryan", department: "Game Development", salary: 120000 },
    { _id: 2, name: "David", department: "Backend Development", salary: 108000 },
    { _id: 3, name: "Rachael", department: "Full Stack Development", salary: 110000 },
    { _id: 4, name: "James", department: "DevOps Development", salary: 110000 },
    { _id: 5, name: "Peter", department: "Game Development", salary: 123000 },
])

In the example above, we explicitly specify the values of the _id column for each document inserted in the collection.

The query above should return:

{
    "acknowledged" :true,
    "insertedIds" : [
1.0,
2.0,
3.0,
4.0,
5.0
    ]
}

Unordered Insert

We can also perform an unordered insert by setting the ordered parameter to false as shown below:

db.employees.insertMany([
    { _id: 1, name: "Ryan", department: "Game Development", salary: 120000 },
    { _id: 2, name: "David", department: "Backend Development", salary: 108000 },
    { _id: 3, name: "Rachael", department: "Full Stack Development", salary: 110000 },
    { _id: 4, name: "James", department: "DevOps Development", salary: 110000 },
    { _id: 5, name: "Peter", department: "Game Development", salary: 123000 },
], {ordered: false})

This is useful when working with duplicate documents.

Conclusion

In this post, we covered the fundamentals of working with MongoDB insertMany() method to add multiple documents to a given collection in a single command.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list