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 List Collections
 

MongoDB List Collections

Priya Pedamkar
Article byPriya Pedamkar

Updated March 24, 2023

mongodb list collection

 

 

What is MongoDB List Collections?

MongoDB List Collection is all about storage for the document. It is similar to tables in Mysql to store the records. MongoDB is a schema-less database so it can store any number of fields to the documents. Users can create n number of documents into the collection, or it can be changed at any time and no need to make changes in the MongoDB database. To create a collection, we should insert Document into it, and it must have fields name and field value. If the collection does not exist, then new will be created automatically when we try to insert the record into it.

Watch our Demo Courses and Videos

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

List of MongoDB Collection

Below are the MongoDB List Collections with the proper example in it:

1. First, We Need to Create a Database

We have created the library as a database. Create collection as books.

Syntax:

db.createCollection(name of collection)

Example #1

Code:

use library

Output:

Create a Database

Code:

db.createCollection(“books”)

Output:

MongoDB List of Collection - 2

Syntax:

show collections

(It will display all collections we have created).

Example #2

Code:

use library

Output:

MongoDB List of Collection - 3

Code:

db.createCollection(“students”)

Output:

MongoDB List of Collection - 4.jpg

Code:

show collections

Output:

MongoDB List of Collection - 4.jpg

2. Insert Single Document to Collection

To insert one value into the collection. We have inserted value into student collection.

Syntax:

db.collection.insertOne([{fields:value}])

Code:

db.student.insertOne({ name: "Abhi", age: 12, subject: [ "maths", "english" ] })

Output:

Document to Collection

3. Insert Multiple Documents to Collection

Below is the syntax mentioned

Syntax:

db.collection.insertMany([{field:value},{field:value}])

Code:

db.student.insertMany([{ name: "Rohan", age: 13, subject: [ "hindi", "geo" ] },{ name: "Akhil", age: 14,  subject: [ "marathi", "history" ], place:"mumbai" }])

Output:

 Insert Multiple Documents

Acknowledge: true (all document inserted successfully) and ObjectId is provided for each document.

4. Query All Document in Collection

Below is the syntax mentioned

Syntax:

db.collection_name.find({})

Code:

db.student.find( {} )

Output:

Query All Document

5. Query Document in Collection Based on Criteria

Below is the syntax mentioned

Syntax:

db.collection_name.find({criteria})

Code:

db.student.find(criteria)

We Have Mentioned Criteria as Cars. We have mentioned two criteria separately, i.e. name and place.

Code:

criteria={name:"Abhi"}

Output:

criteria={name:"Abhi"}

Code:

student.find(criteria)

Output:

student.find

Code:

criteria={place:"mumbai" }

Output:

criteria

Code:

student.find(criteria)

Output:

MongoDB List of Collection -12

6. Projection Document to Collection

We Want to Retrieve for Field. We can observe that we have a projected field for name only. We keep _id=0 which means it will not include fields with 0 in the result.

Syntax:

db.collection_name.find(query_document, projection_document)

To Create Projection: {field1:projection_value, field1:projection_value,…}

Code:

projection_doc={name:1,_id :0}

Output:

 Projection Document

Code:

db.student.find({},projection_doc)

Output:

MongoDB List of Collection -14

7. Update a Single Document

Below is the syntax mentioned

Syntax:

Create criteria = criteria={field}
Db.collection_name.find(criteria)
Update={criteria_value,field_value}
Db.collection_name.update(criteria,update)

Code:

criteria={name:"Abhi" }

Output:

Update a Single Document

Code:

db.student.find(criteria )

Output:

MongoDB List of Collection -16

Code:

update={"name":" Abhi","age":14}

Output:

MongoDB List of Collection -17

Code:

db.student.update(criteria,update)

Output:

MongoDB List of Collection -18

Code:

db.student.find(criteria )

Output:

MongoDB List of Collection -19jpg

8. Update Multiple Document

We should use options as multi=true because to update multiple documents in the collection. We can use $set command to update only one value rather than mentioning all the values.

Code:

criteria={"place":"mumbai"}

Output:

MongoDB List of Collection -20jpg

Code:

db.student.find(criteria).count()

Output:

MongoDB List of Collection -21

Code:

update={$set:{marks:200}}

Output:

MongoDB List of Collection -22

Code:

options={multi:true}

Output:

MongoDB List of Collection -23

Code:

db.student.update(criteria,update,options)

Output:

MongoDB List of Collection -24

Code:

db.student.find(criteria)

Output:

 Update Multiple Document

We can observe in the above image we have set MARKS as 200 for Place=Mumbai.

9. Distinct Value from Collection

Below is the syntax mentioned

Syntax:

db.collection_name.distinct(value)

Code:

db.student.distinct("subject" )

Output:

MongoDB List of Collection -26

10. Total Size of Data in Collection, Presented in Bytes

Below is the syntax mentioned

Syntax:

db.collection_name.totalSize()

Code:

db.student.totalSize() 

Output:

MongoDB List of Collection -27

11. Delete Document from Collection

Below is the syntax mentioned

Syntax:

db.collection_name.remove(criteria)

db.student.find ()

Output:

MongoDB List of Collection -28

Code:

criteria={marks:200}

Output:

MongoDB List of Collection -29

Code:

db.student.find(criteria)

Output:

MongoDB List of Collection -30

Code:

db.student.remove(criteria)

Output:

MongoDB List of Collection -31

Code:

db.student.find()

Output:

MongoDB List of Collection -32

Conclusion

We have learned about collection and document. First, we need to create a database then collection and insert the document into the collection. The document is about field and value pair. We can perform add, delete, update, count operations to the collections. We should be excelled with the collection commands then we can perform any operation of collection to single or multiple documents.

Recommended Articles

This is a guide to MongoDB List Collections. Here we discuss what is MongoDB? and List of MongoDB Collection with their proper syntax and examples. You can also go through our other related articles to learn more –

  1. What is MongoDB?
  2. MongoDB Tools
  3. Advantages of MongoDB
  4. MongoDB create Index
  5. MongoDB Collection | Methods and Examples
  6. Guide to MongoDB Limit()
  7. MongoDB MapReduce | Examples

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