Updated February 28, 2023
Introduction to MongoDB insert
MongoDB insert is used to insert a document in the collection; the default insertion ordered is true in MongoDB. For insert document, there is no need to create a collection first, in MongoDB collection is automatically created with the same name at the time of document insertion into collections. Insert method is essential and useful in MongoDB to insert documents into the collections. We can insert single and multiple documents at one time using insert many methods, using the insertMany method to insert multiple documents into the collection.
Syntax with Parameters
Below is the syntax of MongoDB insert.
- collection_name.insert (document)
- collection_name.insert (< Document or array >)
{
Writeconcern: <document>
Ordered: <boolean>
} )
- collection_name.insertOne (Document)
- collection_name.inserMany (Multiple Documents)
Below is the parameter description of above syntax.
- Collection name: Collection name is defined as insert data into the collection by using the insert method. We can insert single and multiple documents as one time using the insertion and insertMany method.
- Insert: This method is handy to insert the document into the collection. This method insert is used to insert a document in the collection.
- Document: Document is defined as data that we have to insert into the collection using the insert method in MongoDB. We can insert single or multiple documents at one time.
- Write concern: This is an optional parameter of the insert method in MongoDB. Insert method uses a default write concern when we don’t specify any option. If we want to use another write concern, then we need to add to the options parameter.
- Ordered: It is an optional parameter of the insert method in MongoDB. The default value of this parameter in the insert method is true. If the value of the ordered list is true, it will perform an ordered insert of documents.
- Insert one: This method is used to insert a single document into collections. If we want to insert a single document, then we have used this method.
- Insert Many: Insert many methods is used to insert multiple documents into collections. If we want to insert multiple documents at one time, then we have using this method.
How insert Command works in MongoDB?
Below is the working of the insert command in MongoDB.
- MongoDB insert is used to insert a document in the collection.
- For insert data, there is no need to create collection first, in MongoDB collection is automatically created with the same name at the time of data insertion into collections.
The below example shows that we do not need to create collections separately.
Code #1
use testing
Code #2
db.emp_ins.insert ({emp_id: 1, emp_name: "ABC"})
Code #3
db.emp_ins.find ()
Code #4
show collections
In the above example, emp_ins collections were automatically created at the time of document insertion into the collection.
- We can insert single and multiple documents at one time using the insertMany method, using the insertMany method to insert multiple documents into the collection.
- The default insertion ordered is true in MongoDB.If the ordered list’s value is true, then it will perform an ordered insert of documents.
- We have also used a write concern parameter with the method. Write concern is an optional parameter of the insert method.
- This insert method uses a default write concern when we don’t specify any option. If we want to use another write concern, then we need to add the parameter into the options parameter.
Examples to Implement MongoDB insert
Below is the example of implementing the same.
1. Create a collection and insert documents
Below example, we have to create a collection first, and then we have inserting documents into it. We have creating collection names as an employee, and after creating a collection, we have to insert documents in employee collection.
Code #1
db.createCollection ("Employee")
Output:
Code #2
db.Employee.insert ({emp_id: 1, emp_name: "ABC", emp_address: "Mumbai", emp_phone: 1234567890})
db.Employee.insert ({emp_id: 2, emp_name: "PQR", emp_address: "Pune", emp_phone: 1234567890})
db.Employee.find()
Output:
2. Insert documents without creating the collection
Below example, we have not created a collection at the time of document insertion it will automatically be created. At the time of document insertion, student name collection was automatically created.
Code #1
show collections
Code #2
db.createCollection ("Student")
Code #3
db.Student.insert ({stud_id: 1, stud_name: "PQR", stud_address: "Pune", stud_phone: 1234567890})
db.Student.insert ({stud_id: 2, stud_name: "ABC", stud_address: "Mumbai", stud_phone: 1234567890})
db.Student.find()
3. Insert a single document using insertOne method
The below example shows insert the document into the collection by using the insertOne method in MongoDB. In the below example, we have inserted documents into student collection by using the insertOne method.
Code #1
db.Student.insertOne ({stud_id: 3, stud_name: "ABC", stud_address: "Mumbai", stud_phone: 1234567890})
Code #2
db.Student.insertOne ({stud_id: 4, stud_name: "PQR", stud_address: "Mumbai", stud_phone: 1234567890})
Code #3
db.Student.find()
4. Insert multiple documents using insertMany method
The below example shows insert multiple documents at one time into the collection by using the insertMany method in MongoDB. In the below example, we have inserted multiple documents at one time into student collection by using the insertMany method are as follows.
Code #1
db.Student.insertMany ([{stud_id: 5, stud_name: "ABC", stud_address: "Mumbai",
stud_phone: 1234567890}, {stud_id: 6, stud_name: "PQR", stud_address: "Pune",
stud_phone: 1234567890}, {stud_id: 7, stud_name: "XYZ", stud_address: "Pune",
stud_phone: 1234567890}, {stud_id: 8, stud_name: "ABC", stud_address: "Mumbai",
stud_phone: 1234567890}])
Code #2
db.Student.find()
In the above example, we have inserted four documents at one time by using the insertMany method in MongoDB. We have to insert N number of documents by using the insertMany method in MongoDB.
Conclusion
MongoDB insert is used to insert the document in the collection. We have inserted a single document using the insert method or insertOne and multiple documents using the insertMany method. To insert data into a collection, there is no need to create the collection; first, it will automatically create at the time of document insertion.
Recommended Articles
This is a guide to MongoDB insert. Here we discuss an introduction, syntax parameter, how does it work with examples to implement. You can also go through our other related articles to learn more –