//Show database command
show dbs
//Switch to a database
use moviesDB
//insert one document in flight database
db.movies.insertOne({
name:"Wolf of wall street",
year:2009,
genre: ["action","adventure","drama"],
rating:3.5,
description: "Based on the true story of Jordan Belfort, from his rise to a wealthy stock-
broker living the high life to his fall involving crime, corruption and the federal government."
//InsertMany
db.movies.insertMany([{
_id:"Sunshine_2009",
name:"Sunshine",
year:2009,
genre: ["sci-fi","thriller"],
rating:3.5,
description : "A team of international astronauts are sent on a dangerous mission to
reignite the dying Sun with a nuclear fission bomb in 2057."
},
_id: "Forest_2005",
name:"Forest Gump",
year:2005,
genre: ["drama","crime","thriller"],
rating:3,
description: "A team of international astronauts are sent on a dangerous mission to
reignite the dying Sun with a nuclear fission bomb in 2057."
},
_id:"Idiots_2004",
name:"3 Idiots",
year:2004,
genre: ["drama","comedy","romantic"],
rating:4.5,
description: "Two friends are searching for their long lost companion. They revisit their
college days and recall the memories of their friend who inspired them to think differently, even as the
rest of the world called them idiots"
}])
//show collections
show collections
//find data
db.moviesDB.find().pretty()
//Adding our own id
db.movies.insertOne({
_id:"Inception_2006",
name:"Inception",
year:2006,
genre: ["action","adventure","sci-fi"],
rating:4,
description: "A thief who steals corporate secrets through the use of dream-sharing
technology is given the inverse task of planting an idea into the mind of a C.E.O."
//Update a document
db.movies.updateOne({name:"Titanic"},{$set: {"rating":5}})
//Update many documents
db.movies.updateMany({year:2001},{$set: {rating: 3}})
//Add a Field (set operator)
db.movies.updateMany({},{$set: {country: “India”}})
//Remove a Field (unset operator)
db.movies.updateMany({name:”Titanic”},{$unset: {country: “India”}})
//Rename a Field (rename operator)
db.movies.updateMany({},{$rename: {“rating”:”rating_customer”}}
//Replace a document (update without set operator)
db.movies.update({name:”Inception”},{name: “Inception-new”,rating: 4}})
//Delete One document
db.movies.deleteOne({name:”Sunshine”})
//Delete Many
db.movies.deleteMany({year:2001})
//Delete All
db.movies.deleteMany({})
//InsertMany
db.movies.insertMany([ //Copy paste the content of movies document here])
//find with a filter
db.flightData.find({"name":"Titanic"}).pretty()
db.flightData.find({"rating": {$gt:3}}).pretty()
Projections (Happens on MongoDB, just like select in SQL)
db.movies.find({},{name:1}).pretty()
db.movies.find({},{name:1, _id:0}).pretty()
Ordered Inserts
db.moviesNew.insertMany([{_id:"titanic",name:"Titanic",year:2003},
{_id:"sunshine",name:"Sunshine",year:2009}])
db.moviesNew.insertMany([{_id:"titanic",name:"Titanic",year:2002},
{_id:"inception",name:"Inception"}])
db.moviesNew.insertMany([{_id:"titanic",name:"Titanic",year:2002},
{_id:"inception",name:"Inception"}],{ordered:false})
//Drop Collection
db.passengersData.drop()
//Drop Database
db.dropDatabase()
//In & notin Operator
db.movies.find({name: {$in : [“Titanic”,”Inception”,”Sunshine”]}})
db.movies.find({name: {$notin : [“Titanic”,”Inception”,”Sunshine”]}}
//gte & lte Operator
db.movies.find({rating: {$gte : 4}})
db.movies.find({rating: {$lte: 3}}
//and & or Operator
db.movies.find({$or : [{name:”Titanic”},{rating: {$gte:3}}]})
db.movies.find({$and : [{name:”Titanic”},{rating: {$gte:3}}]})
//regex Operator
db.movies.find({description: {$regex: \idiot\}})