MongoDB insertMany()

In MongoDB the db.collection.insertMany() method inserts multiple documents into a collection.

The collection part is the name of the collection to insert the documents into.

Example

Here’s an example of using db.collection.insertMany() to insert multiple documents into a collection called pets:

db.pets.insertMany([
    { _id: 1, name: "Wag", type: "Dog" },
    { _id: 2, name: "Bark", type: "Dog" },
    { _id: 3, name: "Meow", type: "Cat" }
    ])

Result:

{ "acknowledged" : true, "insertedIds" : [ 1, 2, 3 ] } 

The db.collection.insertMany() method returns a document containing:

  • A boolean acknowledged as true if the operation ran with write concern or false if write concern was disabled.
  • An array of _id values for each successfully inserted document.

Now if we use db.collection.find() to look at the collection, we will see the newly added documents.

db.pets.find()

Result:

{ "_id" : 1, "name" : "Wag", "type" : "Dog" }
{ "_id" : 2, "name" : "Bark", "type" : "Dog" }
{ "_id" : 3, "name" : "Meow", "type" : "Cat" }

In this case, our three documents are the only ones in the collection, and so only these documents were returned.

However, if the collection was large, we could use the document IDs to narrow the result to just the documents that we’re interested in.

db.pets.find({_id: {$in: [1,2,3]}})

Result:

{ "_id" : 1, "name" : "Wag", "type" : "Dog" }
{ "_id" : 2, "name" : "Bark", "type" : "Dog" }
{ "_id" : 3, "name" : "Meow", "type" : "Cat" }

What if the Collection Doesn’t Exist?

If the collection doesn’t exist, it is created, and the documents are added to it.

If the collection already exists, then the documents are simply added to it (provided their _id values don’t conflict with any existing ones).

When I created this example, the collection didn’t exist, and so the insert operation created it.

The _id Field

The _id field is a unique identifier field in MongoDB.

As demonstrated in the previous example, you can provide your own _id field in the document. If you do, then its value must be unique within the collection. This is because your _id field will be used as the unique identifier of the document.

Here’s an example of inserting documents without providing the _id fields.

db.pets.insertMany([
    { name: "Bruce", type: "Bat" },
    { name: "Sweetie", type: "Honey Badger" }
    ])

Result:

{
	"acknowledged" : true,
	"insertedIds" : [
		ObjectId("5fe30ef737b49e0faf1af215"),
		ObjectId("5fe30ef737b49e0faf1af216")
	]
}

The _id values of the documents are returned. We could use these to search the collection for just the documents that we inserted.

But in this case, let’s look at the whole collection.

db.pets.find()

Result:

{ "_id" : 1, "name" : "Wag", "type" : "Dog" }
{ "_id" : 2, "name" : "Bark", "type" : "Dog" }
{ "_id" : 3, "name" : "Meow", "type" : "Cat" }
{ "_id" : ObjectId("5fe30ef737b49e0faf1af215"), "name" : "Bruce", "type" : "Bat" }
{ "_id" : ObjectId("5fe30ef737b49e0faf1af216"), "name" : "Sweetie", "type" : "Honey Badger" }

We can see that our two new documents are included in the collection.

The ordered Parameter

The db.collection.insertMany() method also accepts an ordered parameter. This is a boolean parameter with a default value of true.

The ordered parameter specifies whether the insert operation should be ordered or unordered.

If ordered is set to false, documents are inserted in an unordered format and may be reordered by mongod to increase performance.

With ordered inserts, if an error occurs during an insert of one of the documents, MongoDB returns on error without processing the remaining documents in the array.

With unordered inserts, if an error occurs during an insert of one of the documents, MongoDB continues to insert the remaining documents in the array.

More Information

The db.collection.insertMany() method also accepts a writeConcern argument, which describes the level of acknowledgment requested from MongoDB for write operations.

See the MongoDB documentation for db.collection.insertMany() for more information.