MongoDB Insert Documents

In this article, we will see MongoDB Insert Document methods. Create or insert operations add new documents to a collection. If the collection does not currently exist, insert operations will create the collection.

Why _id field in MongoDB Documents?

  • If the document contains an _id field, the _id value must be unique within the collection to avoid duplicate key error.
  • If the document does not specify an _id field, then mongod will add the _id field and assign a unique ObjectId for the document before inserting.

MongoDB provides following methods to insert documents:

  • db.collection.insert()
  • db.collection.insertOne()
  • db.collection.insertMany()

1. MongoDB insertOne():

Inserts a document into a collection. If the collection does not exist, then the insertOne() method creates the collection.

MongoDB insertOne() Syntax:

db.collection.insertOne( , { writeConcern: } )

Where
document: A document to insert into the collection.
writeConcern: A document expressing the write concern.

MongoDB insertOne() Examples:

1. Insert a document without specifying an _id field.

use r2schools db.students.insertOne({'name':'Karunakar','class':4,'gender':'Male'})

2. Insert a document specifying an _id field.

use r2schools db.students.insertOne({'name':'James','class':5,'gender':'Male',_id:101})

3. Lets verify these are successfully inserted or not.

use r2schools db.students.find().pretty()

Output:

{ "_id" : ObjectId("5dea20229a25ba328252744c"), "name" : "Karunakar", "class" : 4, "gender" : "Male" } { "_id" : 101, "name" : "James", "class" : 5, "gender" : "Male" }

2. MongoDB insertMany():

Inserts multiple documents into a collection. If the collection does not exist, then the insertMany() method creates the collection.

MongoDB insertMany() Syntax:

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

Where
document: A document to insert into the collection.
writeConcern: A document expressing the write concern.
ordered: A boolean specifying whether the mongod instance should perform an ordered or unordered insert. Defaults to true.

MongoDB insertMany() Examples:

1. Insert multiple documents without Specifying an _id field.

db.products.insertMany( [ { item: "card", qty: 15 }, { item: "envelope", qty: 20 }, { item: "stamps" , qty: 30 } ] )

2. Insert multiple documents Specifying an _id Field

db.products.insertMany( [ { _id: 10, item: "large box", qty: 20 }, { _id: 11, item: "small box", qty: 55 }, { _id: 12, item: "medium box", qty: 30 } ] )

3. MongoDB insert():

Inserts single or multiple documents into a collection. If the collection does not exist, then the insertMany() method creates the collection.

MongoDB insert() Syntax:

db.collection.insert( , { writeConcern: , ordered: } )

Where
document: A document to insert into the collection.
writeConcern: A document expressing the write concern.
ordered: A boolean specifying whether the mongod instance should perform an ordered or unordered insert. Defaults to true.

MongoDB insert() Examples:

1. Insert a Document without Specifying an _id Field:

db.products.insert( { item: "card", qty: 15 } )

2. Insert a Document Specifying an _id Field:

db.products.insert( { _id: 13, item: "box", qty: 20 } )

3. Insert multiple documents usign insert method:

db.products.insert( [ { _id: 11, item: "pencil", qty: 50, type: "no.2" }, { item: "pen", qty: 20 }, { item: "eraser", qty: 25 } ] )

4. Perform an Unordered Insert using insert method:

db.products.insert( [ { _id: 20, item: "lamp", qty: 50, type: "desk" }, { _id: 21, item: "lamp", qty: 20, type: "floor" }, { _id: 22, item: "bulk", qty: 100 } ], { ordered: false } )

BulkWriteInsert: When passed an array of documents, insert() returns a BulkWriteResult() object.