EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials MongoDB Tutorial MongoDB List Collections
Secondary Sidebar
MongoDB Tutorial
  • Advanced
    • MongoDB Array
    • PostgreSQL ARRAY_AGG()
    • Indexes in MongoDB
    • MongoDB create Index
    • MongoDB Collection
    • MongoDB List Collections
    • MongoDB Capped Collections
    • MongoDB Delete Collection
    • Mongodb show collections
    • MongoDB Auto Increment
    • MongoDB Triggers
    • MongoDB Projection
    • Replication in MongoDB
    • MongoDB Database
    • Mongo DB Create Database
    • MongoDB Compass
    • MongoDB Users
    • MongoDB Authentication
    • MongoDB GridFS
    • MongoDB Relationships
    • MongoDB MapReduce
    • MongoDB Geospatial
    • MongoDB Monitoring
    • Backup in MongoDB
    • MongoDB Sharding
    • MongoDB Java Drivers
    • MongoDB Import
    • Mongo Database Interview Questions
    • MongoDB Join Two Collections
    • MongoDB Group by Multiple Fields
    • MongoDB Pagination
    • MongoDB Replica Set
    • MongoDB Bulk Update
    • MongoDB greater than
    • MongoDB Encryption
    • MongoDB find in array
    • MongoDB like query
    • Mongodb shell
    • MongoDB port
    • MongoDB Query Operators
    • MongoDB Web Interface
    • MongoDB Query Array
    • MongoDB Transactions
    • MongoDB Not In
    • MongoDB not null
    • MongoDB npm
    • MongoDB Remove
  • 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 Data Types
    • MongoDB Administration
    • Data Modeling in MongoDB
    • MongoDB vs Elasticsearch
    • MariaDB vs MongoDB
    • Firebase vs MongoDB
  • Commands
    • Mongodb updateMany
    • MongoDB Aggregation
    • Mongodb unwind
    • Mongodb where
    • MongoDB BSON
    • MongoDB Filter
    • Mongodb Match
    • MongoDB sort by date
    • MongoDB Limit()
    • MongoDB Atlas Login
    • MongoDB Relational Database
    • MongoDB count
    • MongoDB Aggregate
    • MongoDB Distinct
    • MongoDB Unique
    • MongoDB find
    • MongoDB findOne()
    • MongoDB insert
    • MongoDB Delete
    • MongoDB Update
    • Lookup in MongoDB
    • order by in MongoDB
    • MongoDB $regex
    • MongoDB $elemMatch
    • MongoDB ObjectId()
    • MongoDB Skip()
    • MongoDB findAndModify
    • Mongodb findOneAndUpdate
    • MongoDB Date Query
    • MongoDB Timestamp
    • MongoDB sort()
    • MongoDB group by
    • MongoDB Join

Related Courses

MongoDB Certification Course

Oracle Certification Course

All in One Data Science Course

SQL Training Course

Oracle DBA Course

MS SQL Certification Course

MongoDB List Collections

By Priya PedamkarPriya Pedamkar

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.

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:

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

db.createCollection(name of collection)

All in One Data Science Bundle(360+ Courses, 50+ projects)
Python TutorialMachine LearningAWSArtificial Intelligence
TableauR ProgrammingPowerBIDeep Learning
Price
View Courses
360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (86,408 ratings)
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
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
4.5
Price

View Course

Related Courses

Oracle Training (14 Courses, 8+ Projects)4.9
All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
SQL Training Program (7 Courses, 8+ Projects)4.7
Oracle DBA Database Management System Training (2 Courses)4.7
MS SQL Training (16 Courses, 11+ Projects)4.7
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • 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

ISO 10004:2018 & ISO 9001:2015 Certified

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

EDUCBA
Free Data Science Course

SPSS, Data visualization with Python, Matplotlib Library, Seaborn Package

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

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

EDUCBA Login

Forgot Password?

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

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

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

EDUCBA

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

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

Let’s Get Started

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

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