NOTE : We will use the below in the entire document for learning purpose :
DB Name : infra
Collection Name : topics
1. Create a database :
User Infra
Note : Creating a database is only possible, once you add a collection to it
2. Create a collection :
Here we are creating a collection named topics :
db.createCollection(“topics”)
3. Insert into a collection :
Inserting into a collection can in the following ways :
a. db.collection_name.insertOne({..})
b. db.collection_name.insertMany({..})
We can use const function to make our life easier
const doc = [
{
Title: “How many videos are there”,
postID: NumberInt(3718),
comments: 03,
shared: false,
tags: [“AWS DMS”, “MySQL Replication”, “MongoDB”, “Shell Scripting”],
Author: {
Name: “Infra Xpertzz”,
nickname: “IX”
}
},
{
Title: “What other channel do you have?”,
postID: NumberInt(3719),
comments: 07,
shared: true,
tags: [“Travel”, “Horror Animation”],
Author: {
Name: “Ira’s Tales”,
nickname: “IT”
}
}
];
db.topics.insertOne(doc);
db.topics.insertMany(doc);
4. Find documents in collection topics :
Finding a collection can be in the following ways :
a. db.collection_name.find({..}) –> This reads everything in the collection
b. db.collection_name.findOne({..})
5. Count a Collection :
db.topics.count();
db.topics.countDocuments();
db.topics.estimatedDocumentCount();
6. Find a document with 0 comments:
db.topics.findOne({comments: 0});

7. $gt operator :
Finds comments which has values greater than 0 :
db.topics.find({comments:{$gt:0}});

8. $and operator :
Find documents with comments less than 5 and greater than 0 :
db.topics.find({
$and : [
{comments : {$lt : 5}},
{comments : {$gt : 0}}
]
})

9. $or operator :
Find documents with either shared = false or Author name as Ira’s Tales :
Since Author is an object, it needs to be in double quotes :
db.topics.find({ $or : [ {shared : false}, {“Author.Name” : “Ira’s Tales”} ] })

10. $in operator :
Requires an array of elements as a value. And this operator is used for arrays. It will find documents that have certains value in the array that exists in the list of the elements that you pass as a value to this $in operator
Find documents inside topics collection where tags are in AWS DMS, Animation and MongoDB :
db.topics.find({ tags: { $in: [ “AWS DMS”, “Animation”, “MongoDB”] } })

11. Limit Helper Method :
This limits the result to the first 2 documents
db.topics.find({}).limit(2);

12. Sort comments section is ascending order :
db.topics.find({}).sort({comments : 1});
13. Sort comments section is decending order :
db.topics.find({}).sort({comments : -1});
14. Skip helper method :
db.topics.find({}).skip(2);
This skips the first 2 documents
15. Skips 2 elemnets and sort by shared field :
db.topics.find({}).skip(2).sort({shared : 1})
16. $set Operator :
Update a document and set its shared to trued :
db.topics.updateOne( { postID: 3718 }, { $set: { shared: true } } )
17. $unset Operator :
Unset the value of tags and update with 1
db.topics.updateMany( { tags: [] }, { $unset: { tags: 1 } } )

18. Delete Operator with $exists :
db.topics.deleteMany( { tags: { $exists: false } })
Deletes documents which doesn’t have any tags
19. Set Read Preference in Secondary for the current session :
db.getMongo().setReadPref(‘secondary’);
20. List databases :
show dbs
21. Check which database you are in :
db
22. Clear the screen :
cls
23. To read documents in a pretty manner :
db.topics.find().pretty()