NOTES
To Show database cmd – show dbs (will give you result of all
databases).
To use database cmd- use____(the Database name)
ex:-use admin.
To create database cmd – use____(name of database)
ex:- use school.
Note in terminal only databases show which had data rest will not
see.(CLS-for clear screen)
To add collection in the DB cmd - [Link](“students”)
Drop the database cmd- [Link]()
To insert the values in the db cmd –
[Link]({name:”spongbob”, age:30, gpa:3.2})
To show data inDB cmd – [Link]()
Now you can add more then one value using cmd –
[Link]([{name:”patrik”,age:31,gpa:2},{name:”s
andy”, age:20, gpa:4.0},{name:”gery”,age:18,gpa:3.0}])
Data Types in mongodb- string, integer,double, Booleans,date
Ex-to add the date cmd- [Link]({ registerDate:
new Date(“”)}), Null datatype ,
courses[“biology”,”chem.”,”calculus”]-Arrays ,nested docs Ex-
Address:{street:”123 fake st”,city:”bikini bottom”}
Sorting and Limiting
Sorting-to sort the data cmd-
[Link]().sort({name:1}) (1 for alphabetical order
-1 for reverse alphabetical order)
Ex2(bygpa)- [Link]().sort({gpa:1})(1 for ASC , -1 for
DESC)
Limiting- Limit the amount of documents that are return to
us Cmd – [Link]().limit(1)
1- give one documents,2- will return 2 documents,3-will
return 3 documents.
We can also use the sort and limit method combined
Ex- [Link]().sort({gpa:-1}).limit(1) (this will
return the data which had highest gpa in the class)
FIND METHOD
To use this method Cmd- [Link]() (this will return the
all data present in the DB.)
For specific data the Cmd –
[Link]({query},{projection})
Ex:- [Link]({name:”spongbob”})
[Link]({gpa:4.0})
[Link]({},{_id:false,name:true}) (this will return the
names in db only)
[Link]({},{_id:false, name:true, gpa:true})
UPDATE
To update the one document we use Cmd-
[Link](filter,update)
Ex-
[Link]({name:”spongbob”},{$set:{fulltime:tru
e}}) ($set- replace the value of field)
-[Link]({_id: objectid()},{$set:{fulltime:false}})
-[Link]({_id: objectid()},{$unset:{fulltime:””}})
($unset- to remove the field)
Now the updateMany()
-[Link]({filter},{update})
Ex-
[Link]({name:”gary”},{$unset:{fulltime:””}})
To check if the field Exist or not Exist
cmd-
[Link]({fulltime:{$exists:false}},{$set:{fu
lltime:true}})
Delete in Mongodb
There are two methods DeleteOne, DeleteMany.
DeleteOne
Cmd- [Link]({filter})
Ex- [Link]({name:”larry”}) (this will delete the
whole data of the larry)
DeleteMany
[Link]({})
ex- [Link]({fulltime:false})
- [Link]({registerDate:{$exists:false}})
(this will delete the data which don’t had register data first we
check it using the $exists operator is it true or false then the
query will execute).
Comparison Operators
Return data based on the value comparisons and Denote by $.
Ex- [Link]({name:{$ne:“spongbob”}}) (this will return
all value except spongbob ($ne-not equal operator))
-[Link]({age:{$lt:20}}) ( $lt- less then, it will return
the value which had age less then 20 year old)
-[Link]({age:{$lte:20}}) ( $lt- less then equal to, it will
return the value which had ageless than or equal to 20 year old)
-[Link]({age:{$gt:20}}) ( $gt- Greater then, it will
return the value which had age greater than 20 year old)
-[Link]({age:{$gte:20}}) ( $gte- greater then equal to,
it will return the value which had age greater than or equal to
20 year old)
Q Find the no of students which had gpa b/w 3.0-4.0 ?
Cmd- [Link]({gpa:{$gte:3,$lte:4}})
$in- we can return all records that’s have one of the
matching value
cmd-
[Link]({name:{$in:[“spangbob”,”patrik”,”sandy
”]}})
$nin-not in
[Link]({name:{$nin:[“spangbob”,”patrik”,”san
dy”]}})
LOGICAL OPERATORS
Return data based on expressions that evaluate to True or False
$and- [Link]({$and: [{fulltime:true},{age:{lte:22}}]})
(Fulltime AND age<=22)
$or - [Link]({$or: [{fulltime:true},{age:{lte:22}}]})
(Fulltime OR age<=22)
$nor - [Link]({$nor: [{fulltime:true},{age:{lte:22}}]})
(Fulltime NOR age<=22)
$not- [Link]({$not:{age:{$not{gte:30}}})
(age IS NOT>=30)
INDEXES
Ex- [Link]({name:”larry”}).explain(“executionStats”)
(-by linear search this method will examine 5 methods or the all
data records present in the database)
(by indexes)
-[Link]({name: 1}) (this method will
examine 1 method from the all data records present in the
database which is faster)
To get the indexes Cmd- -[Link]()
To drop the indexes Cmd- -
[Link](“nameofindex”)
COLLECTIONS
Collections are the group of documents
To show collections in DB Cmd- show collections
Create Collections of teachers Cmd-
[Link](“teacher”,{capped:true,
size:10000000,max:100},{autoIndexId:false})
To drop the Collections Cmd:- [Link]()
©Dilpreet Singh Sainbhee