Mongo DB

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

MongoDB 2024

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

Insert data to database


Note: If collection is not created yet once you write insert in order to insert data to the database it
will be created
db.students.insertOne({name: "Mohammed", age:30, pga:3.2})

Clear the shell terminal


cls

Return documents within a collection


db.students.find()
Insert many documents into a collection
db.students.insertMany([{name:"john" ,age:33 ,gpa:1.5},{name:"lilan" ,age:27 ,gpa:3.5},
{name:"aldeno" ,age:27 ,gpa:4.5}])

Data type accepted


String name:”Larry”
Integer age:32
Double gpa:2.7
Boolean fullTime: false
Date registerDate: new Date()
NoValue gradutionDate: null
Array courses: [“Biology”, “Chemistry”, “Calculus”]
address:{street:”123 Fake St.”,
city: “Bikini Bottom”,
zip: 12345}

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},
})

Sorting and limiting


Sorting
db.students.find().sort({name: 1})
Note: 1 means alphabetical order
db.students.find().sort({name: -1})
Note: -1 means reverse alphabetical order
db.students.find().sort({gpa: 1})
db.students.find().sort({gpa: -1})

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)

find student with lowest gpa


db.students.find().sort({gpa:1}).limit(1)

Find method (find specific document in database)


db.students.find({query}, {projection})
db.students.find({name:”mohammed”})
db.students.find({gpa:4.5})
db.students.find({fullTime:false})

use more than one filter


db.students.find({gpa:4.5, fullTime:true, graduation:false})

use filter for only one argument


db.products.find({}, {name:true})
Note: this will show out only names

Note: you can reject id argument when it showed with only names filter by
db.products.find({}, {_id:false ,name:true})

here is name and gpa of every student


db.products.find({}, {_id:false ,name:true, gpa: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}})

Remove filed from data


db.students.updateOne({_id: ObjectId('662fbadde8cba0f33146b79b')}, {$unset: {fullTime:""}})

Update many from data


db.students.updateMany({}, {$set: {fullTime:"true"}})

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"}})

Delete document in MongoDB


deleteOne
db.students.deleteOne({name:"mohammed"})

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}})

less than or equal to ($lte)


db.students.find({age: {$lte:27}})

greater than ($gt)


db.students.find({age: {$gt:27}})

greater than or equal ($gte)


db.students.find({age: {$gte:27}})

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"] }})

not in operator ($nin)


Return all documents where name doesn’t have one of these values
db.students.find({name: {$nin:["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}}] } )

db.users.find({$and: [{age: {$lte:25}}, {emp: false}, {fullTime:true}] }, {name:true})

$or
db.students.find( {$or: [{fullTime:true}, {age:{$lte: 27}}] } )

db.users.find({$or: [{status: "single"}, {gender:"Female"}] }, {name:true, status:true,


gender:true})

$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

You might also like