Professional Documents
Culture Documents
Mongo DB
Mongo DB
Mongo DB
DEVELOPED BY
“MOHAMMED Q. SATTAR”
Show Databases
show dbs
Use Database
use databaseName
example use admin
Note: If you write use database which does not exists it will create this database
example use school (school was not list in the database but now it has been created)
to fill database when it is empty
db.createCollection("Students")
Drop Database
db.dropDatabase()
Note: database should be used first to be dropped
db. students.insertOne({
name:”Larry”,
age:32,
gpa:2.7,
fullTime: false,
registerDate: new Date(),
gradutionDate: null,
courses: [“Biology”, “Chemistry”, “Calculus”],
address:{street:”123 Fake St.”,
city: “Bikini Bottom”,
zip: 12345},
})
Limiting
db.students.find().limit(2)
db.students.find().limit(3)
db.students.find().limit(4)
Note: 2 means number of arguments (data) will bring back
Mix them to get highest student gpa and then limit it to 1 only heist will be display
db.students.find().sort({gpa:-1}).limit(1)
Note: you can reject id argument when it showed with only names filter by
db.products.find({}, {_id:false ,name:true})
Note: you can turn any argument you want on or off by passing the argument name and assign
for true or false as top examples
db.products.find({}, {_id:false ,name:true, gpa:true, age:true, phoneNumber:true})
Update method
db.students.updateOne(filter, update)
db.students.updateOne({name: "mohammed"}, {$set: {fullTime:true}})
Note: in case of huge data and would like to update using name of student it is preferred to
update using student id so there will be no same id but name sometimes there are similar names
db.students.updateOne({_id: ObjectId('662fbadde8cba0f33146b79b')}, {$set: {fullTime:false}})
Note: we may need sometimes to update two or three students between others for example if two
students do not have fulltime in their table, but others have, we can give these two students
fulltime argument and set it as we want, (we used the key $exists in this example)
db.students.updateMany({fullTime:{$exists:false}}, {$set: {fullTime:"false"}})
deleteMany
db.students.deleteMany({fullTime:false})
db.students.deleteMany({fullTime:{$exists:false}})
db.students.deleteMany({registrationDate:{$exists:false}})
Comparison operators
Return data based on value comparisons
$ne, $lt, $lte, $gt, $gte, $in, $nin
Not equal ($ne)
Note: to find everybody but not mohammed we can use not equal ($ne)
db.students.find({name: {$ne:"mohammed"}})
less than ($lt)
db.students.find({age: {$lt:30}})
db.students.find({gpa: {$lt:2}})
Mixed operators
db.students.find({gpa: {$gte:3, $lte:4.5}})
$in operator
Return anybody his name within an input array
Return all documents when one the value in this array matches the document filed
db.students.find({name: {$in:["mohammed", "aldeno"] }})
Logical operators
Logical operators return data based on expressions that evaluate to true or false
$and, $not, $nor, $or
$and
db.students.find( {$and: [{fullTime:false}, {age:{$lte: 33}}] } )
$or
db.students.find( {$or: [{fullTime:true}, {age:{$lte: 27}}] } )
$nor
Return data includes both arguments to be false for example if in condition I wrote status married
$nor should return the data that are not married
db.users.find({$nor: [{status: "single"}, {gender:"Male"}] }, {name:true, status:true,
gender:true})
$not
db.users.find({age:{$not:{$gte:30}}})
db.users.find({age:{$not:{$lte:30}}})
db.users.find({gpa:{$not:{$lte:3}}})
Indexes