MongoDB Notes
MongoDB 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}])
-db.students.updateMany({filter},{update})
Ex-
db.students.updateMany({name:”gary”},{$unset:{fulltime:””}})
LOGICAL OPERATORS
Return data based on expressions that evaluate to True or False
$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()