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 - db.createCollection(“students”)
Drop the database cmd- db.dropDatabase()
To insert the values in the db cmd –
db.students.insertOne({name:”spongbob”, age:30, gpa:3.2})
To show data inDB cmd – db.students.find()
Now you can add more then one value using cmd –
db.students.insertMany([{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- db.students.insertOne({ 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-
db.students.find().sort({name:1}) (1 for alphabetical order
-1 for reverse alphabetical order)
Ex2(bygpa)- db.students.find().sort({gpa:1})(1 for ASC , -1 for
DESC)
Limiting- Limit the amount of documents that are return to
us Cmd – db.students.find().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- db.students.find().sort({gpa:-1}).limit(1) (this will
return the data which had highest gpa in the class)
FIND METHOD
To use this method Cmd- db.students.find() (this will return the
all data present in the DB.)
For specific data the Cmd –
db.students.find({query},{projection})
Ex:- db.students.find({name:”spongbob”})
db.students.find({gpa:4.0})
db.students.find({},{_id:false,name:true}) (this will return the
names in db only)
db.students.find({},{_id:false, name:true, gpa:true})
UPDATE
To update the one document we use Cmd-
db.students.updateOne(filter,update)
Ex-
db.students.updateOne({name:”spongbob”},{$set:{fulltime:tru
e}}) ($set- replace the value of field)
-db.students.updateOne({_id: objectid()},{$set:{fulltime:false}})
-db.students.updateOne({_id: objectid()},{$unset:{fulltime:””}})
($unset- to remove the field)
Now the updateMany()
-db.students.updateMany({filter},{update})
Ex-
db.students.updateMany({name:”gary”},{$unset:{fulltime:””}})
To check if the field Exist or not Exist
cmd-
db.students.updateMany({fulltime:{$exists:false}},{$set:{fu
lltime:true}})
Delete in Mongodb
There are two methods DeleteOne, DeleteMany.
DeleteOne
Cmd- db.students.deleteOne({filter})
Ex- db.students.deleteOne({name:”larry”}) (this will delete the
whole data of the larry)
DeleteMany
cmd-db.students.deleteMany({})
ex- db.students.deleteMany({fulltime:false})
- db.students.deleteMany({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- db.student.find({name:{$ne:“spongbob”}}) (this will return
all value except spongbob ($ne-not equal operator))
-db.student.find({age:{$lt:20}}) ( $lt- less then, it will return
the value which had age less then 20 year old)
-db.student.find({age:{$lte:20}}) ( $lt- less then equal to, it will
return the value which had ageless than or equal to 20 year old)
-db.student.find({age:{$gt:20}}) ( $gt- Greater then, it will
return the value which had age greater than 20 year old)
-db.student.find({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- db.students.find({gpa:{$gte:3,$lte:4}})
$in- we can return all records that’s have one of the
matching value
cmd-
db.students.find({name:{$in:[“spangbob”,”patrik”,”sandy
”]}})
$nin-not in
db.students.find({name:{$nin:[“spangbob”,”patrik”,”san
dy”]}})
LOGICAL OPERATORS
Return data based on expressions that evaluate to True or False
$and- db.students.find({$and: [{fulltime:true},{age:{lte:22}}]})
(Fulltime AND age<=22)
$or - db.students.find({$or: [{fulltime:true},{age:{lte:22}}]})
(Fulltime OR age<=22)
$nor - db.students.find({$nor: [{fulltime:true},{age:{lte:22}}]})
(Fulltime NOR age<=22)
$not- db.students.find({$not:{age:{$not{gte:30}}})
(age IS NOT>=30)
INDEXES
Ex- db.students.find({name:”larry”}).explain(“executionStats”)
(-by linear search this method will examine 5 methods or the all
data records present in the database)
(by indexes)
-db.students.createIndexes({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- -db.students.getIndexes()
To drop the indexes Cmd- -
db.students.dropIndexes(“nameofindex”)
COLLECTIONS
Collections are the group of documents
To show collections in DB Cmd- show collections
Create Collections of teachers Cmd-
db.createCollection(“teacher”,{capped:true,
size:10000000,max:100},{autoIndexId:false})
To drop the Collections Cmd:- db.teachers.drop()
©Dilpreet Singh Sainbhee