Mongo Commands1
Mongo Commands1
show dbs
//Switch to a database
use moviesDB
db.movies.insertOne({
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,
},
_id: "Forest_2005",
name:"Forest Gump",
year:2005,
genre: ["drama","crime","thriller"],
rating:3,
},
_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()
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}})
db.movies.updateMany({},{$rename: {“rating”:”rating_customer”}}
db.movies.deleteOne({name:”Sunshine”})
//Delete Many
db.movies.deleteMany({year:2001})
//Delete All
db.movies.deleteMany({})
//InsertMany
db.flightData.find({"name":"Titanic"}).pretty()
db.flightData.find({"rating": {$gt:3}}).pretty()
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()
//regex Operator