Inserting Documents in MongoDB

Inserting Documents in MongoDB with Examples

In this article, I am going to discuss Inserting Documents in MongoDB with Examples. Please read our previous article where we discussed How MongoDB Works.

Inserting Documents in MongoDB

MongoDB documents are also known as BSON documents that are used to store data. The document is created using field-value pairs or key-value pairs and the value is of any BSON type. Here BSON document means the binary representation of JSON document.

To insert a document in the MongoDB Database we use the insert operation. It will insert a new document in the specified collection. If the specified collection does not exist, then the insert operation will also create a new collection by adding a new document to it. To insert a document, we can use any of the following methods:

db.collection.insertOne()
db.collection.insertMany()

Inserting Single Document in MongoDB

db.collection.insertOne() method is used to insert a single document in the collection.

  • You are allowed to add a document with or without the _id field. If you insert a document without an _id field, then MongoDB will automatically assign an _id field to the document with a unique objectId.
  • The value of the _id field must be unique inside the collection to avoid getting duplicate key errors. _id key is just like the primary key in SQL.
  • This function is not compatible with db.collection.explain().
  • You can use this function inside the multi-document transactions.
  • When an error occurs, this function will throw either writeError or writeConcernError exception.

Syntax: db.collection.insertOne(<document>, {writeConcern: <document>})

Parameter:

This function takes two parameters:

  1. Document: It represents the document that we want to insert into the database.
  2. writeConcern: It is an optional parameter. It removes the use of default write concern.
Return value:

This function will return a document that contains the following fields with values:

  1. acknowledged: The value of this field is true when the insert operation ran with write concern. Otherwise, it will return false.
  2. insertedId: The value of this field is the _id value of the inserted document.
Example- Inserting Single Document without _id field in MongoDB

Here, in this example, we insert a single document in the company database without the _id field.

db.company.insertOne({“Name”: “Moti”, “Group”:”D”, “Division”:345})

Example- Inserting Single Document without _id field in MongoDB

Example – Inserting single document with _id field in MongoDB

Here, in this example, we insert a single document in the company database with the _id field.

db.company.insertOne({_id: 23456, “Name”: “Soma”, “Group”:”C”, “Division”:39})

Example - Inserting single document with _id field in MongoDB

Inserting Multiple Documents in MongoDB

db.collection.insertMany() method is used to insert multiple documents in the collection. In this method, multiple documents are inserted in an array.

  • You are allowed to add a document with or without the _id field. If you insert a document without an _id field, then MongoDB will automatically assign an _id field to the document with a unique objectId.
  • The value of the _id field must be unique inside the collection to avoid getting duplicate key errors. _id key is just like the primary key in SQL.
  • Documents are inserted in order format. If the value of the ordered parameter is false, then only documents are inserted in an unordered format.
  • This function is not compatible with db.collection.explain().
  • You can use this function inside the multi-document transaction.
  • When an error occurs, this function will throw a BulkWriteError exception. It also throws either writeError or writeConcernError exception.

Syntax: db.collection.insertMany([<document1>,<document2>..], {writeConcern: <document>, ordered: <boolean>})

Parameter:

This function takes two parameters:

  1. Documents: It represents an array of documents that we want to insert into the database.
  2. writeConcern: It is an optional parameter. It removes the use of default write concern.
  3. ordered: It is also an optional parameter. It is used to tell mongod or mongosh to insert documents in ordered or unordered format. The default value of this parameter is true.
Return value:

This function will return a document that contains the following fields with values:

  1. acknowledged: The value of this field is true when the insert operation ran with write concern. Otherwise, it will return false.
  2. insertedId array: The value of this field is the _id value of the successfully inserted documents.
Example – Inserting a single document without the _id field in MongoDB

Here, in this example, we insert a single document in the company database without the _id field.

db.company.insertMany([{Name: “Monti”, Group: “A”, Division: 34}, {Name: “Push”, Group:”C”, Division: 39}])

Inserting Multiple Documents in MongoDB

Example – Inserting single document with _id field in MongoDB

Here, in this example, we insert a single document in the company database with the _id field.

db.company.insertMany([{_id:234, Name: “Somiya”, Group: “A”, Division: 34}, {_id:123, Name: “Sonil”, Group:”D”, Division: 348},{_id:235, Name:”Ponit”, Group: “F”, Division: 344}])

Example - Inserting single document with _id field in MongoDB

In the next article, I am going to discuss Replace Document in MongoDB with Examples. Here, in this article, I try to explain Inserting Documents in MongoDB with Examples. I hope you enjoy this Inserting Documents in MongoDB with Examples article.

Leave a Reply

Your email address will not be published. Required fields are marked *