EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login

MongoDB insert

By Priya PedamkarPriya Pedamkar

Home » Data Science » Data Science Tutorials » MongoDB Tutorial » MongoDB insert

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.

Syntax with Parameters

Below is the syntax of MongoDB insert.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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

Popular Course in this category
MongoDB Training Program (4 Courses, 2 Projects)4 Online Courses | 2 Hands-on Projects | 22+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,421 ratings)
Course Price

View Course

Related Courses
Oracle Training (14 Courses, 8+ Projects)All in One Data Science Bundle (360+ Courses, 50+ projects)SQL Training Program (7 Courses, 8+ Projects)Oracle DBA Database Management System Training (2 Courses)MS SQL Training (13 Courses, 11+ Projects)

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

MongoDB Training Program (4 Courses, 2 Projects)

4 Online Courses

2 Hands-on Projects

22+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
MongoDB Tutorial
  • Commands
    • MongoDB Limit()
    • MongoDB count
    • MongoDB Aggregate
    • MongoDB Distinct
    • MongoDB Unique
    • MongoDB find
    • MongoDB insert
    • MongoDB Delete
    • MongoDB Update
    • Lookup in MongoDB
    • order by in MongoDB
    • MongoDB $regex
    • MongoDB $elemMatch
    • MongoDB ObjectId()
    • MongoDB Skip()
    • MongoDB Date Query
    • MongoDB Timestamp
    • MongoDB sort()
    • MongoDB group by
    • MongoDB Join
  • Basics
    • What is MongoDB
    • How To Install MongoDB
    • MongoDB Tools
    • MongoDB GUI Tools
    • MongoDB Versions
    • MongoDB Commands
    • Advantages of MongoDB
    • MongoDB Features
    • Is MongoDB NoSQL
    • Is MongoDB Open Source
    • Build Web Applications using MongoDB
    • MongoDB vs Elasticsearch
    • MariaDB vs MongoDB
    • Firebase vs MongoDB
  • Advanced
    • MongoDB Array
    • PostgreSQL ARRAY_AGG()
    • Indexes in MongoDB
    • MongoDB create Index
    • MongoDB Collection
    • MongoDB List Collections
    • MongoDB Capped Collections
    • MongoDB Auto Increment
    • MongoDB Projection
    • Replication in MongoDB
    • Mongo DB Create Database
    • MongoDB Compass
    • MongoDB Users
    • MongoDB Authentication
    • MongoDB GridFS
    • MongoDB Relationships
    • MongoDB Monitoring
    • Backup in MongoDB
    • MongoDB Sharding
    • MongoDB Java Drivers
    • MongoDB Import
    • Mongo Database Interview Questions

Related Courses

MongoDB Certification Course

Oracle Certification Course

All in One Data Science Course

SQL Training Course

Oracle DBA Course

MS SQL Certification Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • 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

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

EDUCBA Login

Forgot Password?

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

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

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

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

Special Offer - MongoDB Training Program (4 Courses, 2 Projects) Learn More