How to use MongoDB insertOne and insertMany queries

MongoDB is a document-oriented database. It is an open-source database frequently referred to as a NoSQL database.

A document is a basic or small unit. In MongoDB, a document is a group of data in a record similar to a row in the relational database. But MongoDB is a non-relational database that provides support for JSONJSON stands for JavaScript Object Notation. It is a lightweight format for storing data that is human-readable. data.

The basic operations of any database include CRUD. CRUD operations stand for creating, reading, updating, and deleting documents. A thorough explanation of the creation operation in MongoDB is given below:

Creation

Create or insert operations are used to add new documents to the collection. There are three methods to insert new documents in MongoDB:

  1. db.collection.insert()

  2. db.collection.insertOne()

  3. db.collection.insertMany()

Note: In MongoDb, if the collection doesn't exist before these insertion queries, it creates new a collection automatically and inserts given data.

Let's discuss all of the creation methods in detail:

The insert()method

This method adds a new document or array of documents to the collection. The syntax of insert() query is:

db.collection.insert(
<document or array of documents>,
{
writeConcern: <document>,
ordered: <boolean>
}
)

Parameters

The descriptions of the parameters are given below:

  • document: This is a document or array of documents to insert into the collection.

  • writeConcern: This is a document defining the write concern, but it is optional. We omit it to use the default write concern.

  • ordered: If true, execute an ordered insert of the array's documents. if an error is encountered with one of the documents, MongoDB will terminate the operation without processing the array's other documents. If it does an unordered insert. And if any document has an error, it continues processing the array's remaining documents.

Return value

It returns an object that includes the status of the operation.

Code example

Let's look at an example of insert() query in MongoDB:

db.courses.insert(
//inserting course name "c++" with duration of 15 hours into collection "courses"
{
course_name: "C++",
hours: 15
}
)

Note: The Collection.insert() is deprecated that's why we use insertOne() and insertMany() in newer versions.

The insertOne() method

This method adds a single document to the collection. The syntax of insertOne() query is given below:

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

Parameters

The descriptions of the parameters are given below:

  • document : This is a document to insert into the collection.

  • writeConcern : This is a document that defines the written concern, but it is optional. We omit it to use the default write concern.

Return value

It returns the insert_id of the document inserted.

Code example

Let's look at an example of insertOne() query in MongoDB:

db.courses.insertOne(
//inserting course name "python" with duration of 10 hours into collection "courses"
{
course_name: "python",
hours: 10
}
)

This operation returns the following document:

{
acknowledged: true,
insertedId: ObjectId("62eb7b4b3f3139e36a8c85ca")
}

Note: If the documents don't have _id field, MongoDB creates and inserts the _id field for each document giving it a unique ObjectId() value.

The insertMany() method

This method adds many documents or an array of documents to the collection. The syntax of insertMany() query is:

db.collection.insertMany(
[ <document 1> , <document 2>, ... ],
{
writeConcern: <document>,
ordered: <boolean>
}
)

Parameters

The descriptions of the parameters are given below:

  • document : This is the array of documents to insert into the collection.

  • writeConcern : This is the document that defines the written concern, but it is optional. We omit it to use the default write concern.

  • ordered : If true, it executes an ordered insert of the array's documents. If an error is encountered with one of the documents, MongoDB will terminate the operation without processing the array's other documents. If false, it does an unordered insert. And if any document has an error, it continues processing the array's remaining documents.

Return value

It returns inserted_ids of the documents inserted.

Code example

Let's look at an example of insertMany() query in MongoDB:

db.courses.insertMany( [
//inserting course names with duration time into collection "courses"
{ _id: 10, course_name: "python", hours: 10 },
{ _id: 11,course_name: "C++", hours: 15 },
{ _id: 12,course_name: "java", hours: 12 } ]
)

This operation returns the following document:

{
acknowledged: true,
insertedIds: { '0': 10, '1': 11, '2': 12 }
}

Run your queries

We can run all the previous MongoDB queries in the terminal below:

Terminal 1
Terminal
Loading...

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved