EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • 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.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

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

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - 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
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
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 Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW