MongoDB - Insert Single Document in a Collection using insertOne()

In MongoDB, a collection represents a table in RDBMS and a document is like a record in a table. Learn how to insert a single document into a collection.

MongoDB provides the following methods to insert documents into a collection:

  1. insertOne() - Inserts a single document into a collection.
  2. insert() - Inserts one or more documents into a collection.
  3. insertMany() - Insert multiple documents into a collection.

insertOne()

Use the db.<collection>.insertOne() method to insert a single document in a collection. db points to the current database, <collection> is existing or a new collection name.

Syntax:

db.collection.insertOne(document, [writeConcern])

Parameters:

  1. document: A document to insert into the collection.
  2. writeConcern: Optional. A document expressing the write concern to override the default write concern.

The following inserts a document into employees collection.

Example: insertOne()
db.employees.insertOne({ 
    firstName: "John",
    lastName: "King",
    email: "[email protected]"
})
Output
{
  acknowledged: true,
  insertedId: ObjectId("616d44bea861820797edd9b0")
}

In the above example, we passed a document to the insertOne() method. Notice that we haven't specified _id field. So, MongoDB inserts a document to a collection with the auto-generated unique _id field. It returns an object with a boolean field acknowledged that indicates whether the insert operation is successful or not, and insertedId field with the newly inserted _id value.

The following shows the insert operation in the mongosh shell:

InsertOne() in mongosh Shell

Use the find() to list all data of a collection, and the pretty() method to format resulted data.

db.employees.find().pretty()
Output
{
    _id: ObjectId("616d44bea861820797edd9b0"),
    firstName: "John",
    lastName: "King",
    email: "[email protected]"
}

MongoDB is NoSQL database. So, it does not enforce schema to any collection. It means you can insert a document with any fields to a collection. For example, the following inserts a document with different fields to the employees collection.

Example: Insert a Document
db.employees.insertOne({ 
    fName: "John",
    lName: "King",
    emailid: "[email protected]"
})
Output
{
  acknowledged: true,
  insertedId: ObjectId("546d44bea861820797ed214")
}

It is recommended to keep the field names same in all the documents of a collection to manage them easily.

ADVERTISEMENT

Insert _id Manually

It is not necessary to insert auto-generated _id value. You can manually specify a unique value for the _id field, as shown below.

Example: Insert a Document
db.employees.insertOne({ 
    _id:"1",
    firstName: "John",
    lastName: "King",
    email: "[email protected]"
})
Output
{
  acknowledged: true,
  insertedId: 1
}

Note that while adding your custom value to _id field, a value must be unique; otherwise, it will throw an error. The following tries to add the same _id value.

Example: Insert a Document
db.employees.insertOne({ 
    _id:"1",
    firstName: "John",
    lastName: "King",
    email: "[email protected]"
})
Output
MongoServerError: E11000 duplicate key error collection: humanResourceDB.employees index: _id_ dup key: { _id: "1" }
Want to check how much you know MongoDB?