Beginners MongoDB Cheat-Sheet
Shubham Athawane
Posted on December 7, 2021
What is MongoDB ?
MongoDB is a document-oriented NoSQL
database used for high valume data storage instead of using table and rows as in the traditional relational databses, MongoDB makes use of collections and documents
Then Question arises What is documents
and Collections
?
Documents : Consist of key-value pairs
which are the basic unit of the data in MongoDB.
Collections : Contain sets of documents
and functions which is the equivalent of relational database tables
1.Enlist available databases
ā Show db
2 . To Create a new DB
ā use db-name
3 . See your current working directry
ā db
4 . Delete Database
db.mydb.drop()
5 . Create User
-> db.createUser({
user:'admin',
pws:'root',
roles:["readWrite", "dbAdmin"]
})
6 . Creating Collections
-> db.createCollection('customers');
7 . Showing Collections
-> show collections
8 . Inserting values into collections
-> db.customers.insert({first_name:"shubham", last_name:"Athawane"});
9 . View Collection Records
-> db.customers.find();
//And
-> db.customers.find().pretty();
//Note: pretty() will show you result in json formate
10 . Add new Document in Collections
-> db.customers.insert([{"first_name":"Virat", "last_name":"Kohli"},
{"first_name":"Taylor", "last_name":"Swift"}])
// And -> $set, $inc, $unset
-> db.customers.update({first_name:"Glen"}, {$set:{age:45}})
11 . Rename Document
-> db.customers.update({first_name:"Virat"}, {$rename:{"age":"old"}})
12 . Remove document
-> db.customers.remove({first_name:"Joe"})
13 . find the matching
-> db.customers.findOne({first_name:"Joe"})
14 . Count Rows
-> db.customers.count()
15 . This is Less than/Greater than/ Less than or Eq/Greater than or Eq operators
db.customers.find({age: {$lt: 90}})
db.customers.find({age: {$lte: 90}})
db.customers.find({age: {$gt: 90}})
db.customers.find({age: {$gte: 90}})
Posted on December 7, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.