EDUCBA

EDUCBA

MENUMENU
  • Explore
    • Lifetime Membership
    • All in One Bundles
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign up
Home Data Science Data Science Tutorials MongoDB Tutorial MongoDB insert

MongoDB insert

Priya Pedamkar
Article byPriya Pedamkar

Updated February 28, 2023

MongoDB insert

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.

ADVERTISEMENT
Popular Course in this category
MONGODB Course Bundle - 6 Courses in 1

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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

use testing

Code #2

db.emp_ins.insert ({emp_id: 1, emp_name: "ABC"})

write result

Code #3

db.emp_ins.find ()

object id

Code #4

show collections

emp

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:

MongoDB insert - 5

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:

MongoDB insert - 6

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

MongoDB insert - 7

Code #2

db.createCollection ("Student")

MongoDB insert - 8

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()

MongoDB insert - 9

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})

MongoDB insert - 10

Code #2

db.Student.insertOne ({stud_id: 4, stud_name: "PQR", stud_address: "Mumbai", stud_phone: 1234567890})

MongoDB insert - 11

Code #3

db.Student.find()

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}])

acknowledge

Code #2

db.Student.find()

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 –

  1. Lookup in MongoDB
  2. MongoDB List Collections
  3. Indexes in MongoDB
  4. How To Install MongoDB
  5. MongoDB Unique | Examples
  6. Guide to MongoDB Projection
  7. MongoDB Update | How to Work?
  8. MongoDB vs Elasticsearch with Infographics
ADVERTISEMENT
C++ PROGRAMMING Course Bundle - 9 Courses in 1 | 5 Mock Tests
40+ Hour of HD Videos
9 Courses
5 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
ADVERTISEMENT
ASP.NET Course Bundle - 28 Courses in 1 | 5 Mock Tests
123+ Hours of HD Videos
28 Courses
5 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
ADVERTISEMENT
SQL Course Bundle - 51 Courses in 1 | 6 Mock Tests
205+ Hours of HD Videos
51 Courses
6 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
ADVERTISEMENT
SOFTWARE TESTING Course Bundle - 13 Courses in 1
53+ Hour of HD Videos
13 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

🚀 Extended Cyber Monday Price Drop! All in One Universal Bundle (3700+ Courses) @ 🎁 90% OFF - Ends in ENROLL NOW