Visit complete MongoDB roadmap

← Back to Topics List

insert() and relevant

In MongoDB, collections are used to store documents. To add data into these collections, MongoDB provides two primary insertion methods: insertOne() and insertMany(). In this section, we’ll explore the usage and syntax of these methods, along with their options and some basic examples.

insertOne()

The insertOne() method is used to insert a single document into a collection. This method returns an InsertOneResult object, that shows the outcome of the operation.

Syntax:

db.collection.insertOne(
   <document>,
   {
      writeConcern: <document>,
      ordered: <boolean>,
      bypassDocumentValidation: <boolean>,
      comment: <any>
   }
)

Options:

  • writeConcern: An optional document specifying the level of acknowledgment requested from MongoDB for the write operation.
  • ordered: An optional boolean flag. When set to true, MongoDB will return an error if it encounters a duplicate document in the operation. Default is also true.
  • bypassDocumentValidation: Optional boolean flag. To validate or not to validate the document against the collection’s validation rules. Default is false.
  • comment: An optional string or BSON that can be used for descriptive purposes when profiling operations.

Example:

db.inventory.insertOne({
  item: 'book',
  qty: 1,
});

insertMany()

The insertMany() method is used to insert multiple documents into a collection at once. It returns an InsertManyResult object, displaying the status of the operation.

Syntax:

db.collection.insertMany(
   [ <document_1>, <document_2>, ... ],
   {
      writeConcern: <document>,
      ordered: <boolean>,
      bypassDocumentValidation: <boolean>,
      comment: <any>
   }
)

Options:

  • writeConcern: Same as mentioned in insertOne() method.
  • ordered: Same as mentioned in insertOne() method. When set to true, MongoDB will insert the documents in the array’s order. If a fail occurs, it will stop further processing of the documents. Default is true.
  • bypassDocumentValidation: Same as mentioned in insertOne() method.
  • comment: Same as mentioned in insertOne() method.

Example:

db.inventory.insertMany([
  { item: 'pen', qty: 5 },
  { item: 'pencil', qty: 10 },
  { item: 'notebook', qty: 25 },
]);

In conclusion, insert methods in MongoDB allow users to add documents to a collection with a few simple commands. By understanding the syntax and options available for insertOne() and insertMany(), we can efficiently store and manage data within MongoDB collections.

Community

roadmap.sh is the 6th most starred project on GitHub and is visited by hundreds of thousands of developers every month.

Roadmaps Best Practices Guides Videos Store YouTube

roadmap.sh by Kamran Ahmed

Community created roadmaps, articles, resources and journeys to help you choose your path and grow in your career.

© roadmap.sh · FAQs · Terms · Privacy

ThewNewStack

The leading DevOps resource for Kubernetes, cloud-native computing, and the latest in at-scale development, deployment, and management.