0% found this document useful (0 votes)
7 views5 pages

Mongo Commands1

The document provides a series of MongoDB commands for managing a movie database, including commands to show databases, switch databases, insert documents, update, delete, and query data. It demonstrates how to use various operators such as set, unset, rename, and projections, as well as how to handle ordered and unordered inserts. Additionally, it includes examples of using logical operators and regular expressions for filtering data.

Uploaded by

Leonardo Haymal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views5 pages

Mongo Commands1

The document provides a series of MongoDB commands for managing a movie database, including commands to show databases, switch databases, insert documents, update, delete, and query data. It demonstrates how to use various operators such as set, unset, rename, and projections, as well as how to handle ordered and unordered inserts. Additionally, it includes examples of using logical operators and regular expressions for filtering data.

Uploaded by

Leonardo Haymal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 5

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy