Updated March 24, 2023
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:
db.createCollection(name of collection)
Example #1
Code:
use library
Output:
Code:
db.createCollection(“books”)
Output:
Syntax:
show collections
(It will display all collections we have created).
Example #2
Code:
use library
Output:
Code:
db.createCollection(“students”)
Output:
Code:
show collections
Output:
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:
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:
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:
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:
Code:
student.find(criteria)
Output:
Code:
criteria={place:"mumbai" }
Output:
Code:
student.find(criteria)
Output:
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:
Code:
db.student.find({},projection_doc)
Output:
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:
Code:
db.student.find(criteria )
Output:
Code:
update={"name":" Abhi","age":14}
Output:
Code:
db.student.update(criteria,update)
Output:
Code:
db.student.find(criteria )
Output:
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:
Code:
db.student.find(criteria).count()
Output:
Code:
update={$set:{marks:200}}
Output:
Code:
options={multi:true}
Output:
Code:
db.student.update(criteria,update,options)
Output:
Code:
db.student.find(criteria)
Output:
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:
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:
11. Delete Document from Collection
Below is the syntax mentioned
Syntax:
db.collection_name.remove(criteria)
db.student.find ()
Output:
Code:
criteria={marks:200}
Output:
Code:
db.student.find(criteria)
Output:
Code:
db.student.remove(criteria)
Output:
Code:
db.student.find()
Output:
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 –