Task - MongoDB - CRUD Operations #45
Replies: 63 comments 8 replies
Query / Find Documents> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
> use mongo_practice
switched to db mongo_practice
> db.movies.insertMany([{title: "Fight Club", writer: "Chuck Palahniuk", year: 1999, actors: ["Brad Pitt", "EEdward Norton"]},
{title: "Pulp Fiction", writer: "Quentin Tarantino", year: 1994, actors: ["John Travolta", "Uma Thurman"]},
{title: "Inglorius Basterds", writer: "Quentin Tarantino", year: 2009, actors: ["Brad Pitt", "Diane Kruger", "Eli Roth"]},
{title: "The Hobbit: An Unexpected Journey", writer: "J.R.R. Tolkein", year: 2012, franchise: "The Hobbit"},
{title: "The Hobbit: The Desolation of smug", writer: "J.R.R. Tolkein", year: 2013, franchise: "The Hobbit"},
{title: "The Hobbit: The Battle of the Five Armies", writer: "J.R.R. Tolkein", year: 2012, franchise: "The Hobbit",
synopsis: "Bilbo and Company are forced to engage in a war against an array of combatants and keep the the lonely mountains from falling into the hands of a rising darkness."},
{title: "Pee Wee Herman's Big Adventure"}, {title: "Avatar"}]);
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("62a78b4b7960f45ec00b89bb"),
ObjectId("62a78b4b7960f45ec00b89bc"),
ObjectId("62a78b4b7960f45ec00b89bd"),
ObjectId("62a78b4b7960f45ec00b89be"),
ObjectId("62a78b4b7960f45ec00b89bf"),
ObjectId("62a78b4b7960f45ec00b89c0"),
ObjectId("62a78b4b7960f45ec00b89c1"),
ObjectId("62a78b4b7960f45ec00b89c2")
]
}
> db.movies.find({}).pretty();
{
"_id" : ObjectId("62a78b4b7960f45ec00b89bb"),
"title" : "Fight Club",
"writer" : "Chuck Palahniuk",
"year" : 1999,
"actors" : [
"Brad Pitt",
"EEdward Norton"
]
}
{
"_id" : ObjectId("62a78b4b7960f45ec00b89bc"),
"title" : "Pulp Fiction",
"writer" : "Quentin Tarantino",
"year" : 1994,
"actors" : [
"John Travolta",
"Uma Thurman"
]
}
{
"_id" : ObjectId("62a78b4b7960f45ec00b89bd"),
"title" : "Inglorius Basterds",
"writer" : "Quentin Tarantino",
"year" : 2009,
"actors" : [
"Brad Pitt",
"Diane Kruger",
"Eli Roth"
]
}
{
"_id" : ObjectId("62a78b4b7960f45ec00b89be"),
"title" : "The Hobbit: An Unexpected Journey",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit"
}
{
"_id" : ObjectId("62a78b4b7960f45ec00b89bf"),
"title" : "The Hobbit: The Desolation of smug",
"writer" : "J.R.R. Tolkein",
"year" : 2013,
"franchise" : "The Hobbit"
}
{
"_id" : ObjectId("62a78b4b7960f45ec00b89c0"),
"title" : "The Hobbit: The Battle of the Five Armies",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : "Bilbo and Company are forced to engage in a war against an array of combatants and keep the the lonely mountains from falling into the hands of a rising darkness."
}
{
"_id" : ObjectId("62a78b4b7960f45ec00b89c1"),
"title" : "Pee Wee Herman's Big Adventure"
}
{ "_id" : ObjectId("62a78b4b7960f45ec00b89c2"),
"title" : "Avatar"
}
> db.movies.find( { writer : { $eq : "Quentin Tarantino" }}).pretty();
{
"_id" : ObjectId("62a78b4b7960f45ec00b89bc"),
"title" : "Pulp Fiction",
"writer" : "Quentin Tarantino",
"year" : 1994,
"actors" : [
"John Travolta",
"Uma Thurman"
]
}
{
"_id" : ObjectId("62a78b4b7960f45ec00b89bd"),
"title" : "Inglorius Basterds",
"writer" : "Quentin Tarantino",
"year" : 2009,
"actors" : [
"Brad Pitt",
"Diane Kruger",
"Eli Roth"
]
}
> db.movies.find( { actors : { $in : ["Brad Pitt"]}}).pretty();
{
"_id" : ObjectId("62a78b4b7960f45ec00b89bb"),
"title" : "Fight Club",
"writer" : "Chuck Palahniuk",
"year" : 1999,
"actors" : [
"Brad Pitt",
"EEdward Norton"
]
}
{
"_id" : ObjectId("62a78b4b7960f45ec00b89bd"),
"title" : "Inglorius Basterds",
"writer" : "Quentin Tarantino",
"year" : 2009,
"actors" : [
"Brad Pitt",
"Diane Kruger",
"Eli Roth"
]
}
> db.movies.find( { franchise : { $eq : "The Hobbit" }}).pretty();
{
"_id" : ObjectId("62a78b4b7960f45ec00b89be"),
"title" : "The Hobbit: An Unexpected Journey",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit"
}
{
"_id" : ObjectId("62a78b4b7960f45ec00b89bf"),
"title" : "The Hobbit: The Desolation of smug",
"writer" : "J.R.R. Tolkein",
"year" : 2013,
"franchise" : "The Hobbit"
}
{
"_id" : ObjectId("62a78b4b7960f45ec00b89c0"),
"title" : "The Hobbit: The Battle of the Five Armies",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : "Bilbo and Company are forced to engage in a war against an array of combatants
and keep the the lonely mountains from falling into the hands of a rising darkness."
}
> db.movies.find({ $and : [ {year : { $gte :1990 }}, { year :{ $lte : 1999 }} ] }).pretty();
{
"_id" : ObjectId("62a78b4b7960f45ec00b89bb"),
"title" : "Fight Club",
"writer" : "Chuck Palahniuk",
"year" : 1999,
"actors" : [
"Brad Pitt",
"EEdward Norton"
]
}
{
"_id" : ObjectId("62a78b4b7960f45ec00b89bc"),
"title" : "Pulp Fiction",
"writer" : "Quentin Tarantino",
"year" : 1994,
"actors" : [
"John Travolta",
"Uma Thurman"
]
}
> db.movies.find({ $or : [ {year : { $gt :2010 }}, { year :{ $lt : 2000 }} ] }).pretty();
{
"_id" : ObjectId("62a78b4b7960f45ec00b89bb"),
"title" : "Fight Club",
"writer" : "Chuck Palahniuk",
"year" : 1999,
"actors" : [
"Brad Pitt",
"EEdward Norton"
]
}
{
"_id" : ObjectId("62a78b4b7960f45ec00b89bc"),
"title" : "Pulp Fiction",
"writer" : "Quentin Tarantino",
"year" : 1994,
"actors" : [
"John Travolta",
"Uma Thurman"
]
}
{
"_id" : ObjectId("62a78b4b7960f45ec00b89be"),
"title" : "The Hobbit: An Unexpected Journey",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit"
}
{
"_id" : ObjectId("62a78b4b7960f45ec00b89bf"),
"title" : "The Hobbit: The Desolation of smug",
"writer" : "J.R.R. Tolkein",
"year" : 2013,
"franchise" : "The Hobbit"
}
{
"_id" : ObjectId("62a78b4b7960f45ec00b89c0"),
"title" : "The Hobbit: The Battle of the Five Armies",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : "Bilbo and Company are forced to engage in a war against an array of combatants
and keep the the lonely mountains from falling into the hands of a rising darkness."
}Update Documents> db.movies.updateOne({"title":"The Hobbit: An Unexpected Journey"},{$set:{"synopsis":"A reluctant hobbit, Bilbo Baggins, sets out to the Lonely Mountain with a spirited group of dwarves to reclaim their mountain home - and the gold within it - from the dragon Smaug."}});
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
> db.movies.updateOne({"title":"The Hobbit: The Desolation of smug"},{$set:{"synopsis":"The dwarves, along with Bilbo Baggins and Gandalf the Grey, continue their quest to reclaim Erebor, their homeland, from Smaug. Bilbo Baggins is in possession of a mysterious and magical ring."}});
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
> db.movies.updateOne({"title":"Pulp Fiction"},{$push:{"actors":"Samuel L. Jackson"}});
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }Text Search> db.movies.find({"synopsis":{"$regex":"Bilbo"}}).pretty();
{
"_id" : ObjectId("62a78b4b7960f45ec00b89be"),
"title" : "The Hobbit: An Unexpected Journey",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : "A reluctant hobbit, Bilbo Baggins, sets out to the Lonely Mountain with a spirited group of dwarves to reclaim their mountain home - and the gold within it - from the dragon Smaug."
}
{
"_id" : ObjectId("62a78b4b7960f45ec00b89bf"),
"title" : "The Hobbit: The Desolation of smug",
"writer" : "J.R.R. Tolkein",
"year" : 2013,
"franchise" : "The Hobbit",
"synopsis" : "The dwarves, along with Bilbo Baggins and Gandalf the Grey, continue their quest to reclaim Erebor, their homeland, from Smaug. Bilbo Baggins is in possession of a mysterious and magical ring."
}
{
"_id" : ObjectId("62a78b4b7960f45ec00b89c0"),
"title" : "The Hobbit: The Battle of the Five Armies",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : "Bilbo and Company are forced to engage in a war against an array of combatants and keep the the lonely mountains from falling into the hands of a rising darkness."
}
> db.movies.find({"synopsis":{"$regex":"Gandalf"}}).pretty();
{
"_id" : ObjectId("62a78b4b7960f45ec00b89bf"),
"title" : "The Hobbit: The Desolation of smug",
"writer" : "J.R.R. Tolkein",
"year" : 2013,
"franchise" : "The Hobbit",
"synopsis" : "The dwarves, along with Bilbo Baggins and Gandalf the Grey, continue their quest to reclaim Erebor, their homeland, from Smaug. Bilbo Baggins is in possession of a mysterious and magical ring."
}
> db.movies.find({"synopsis":{"$regex":"Bilbo",$not:{"$regex":"Gandalf"}}}).pretty();
{
"_id" : ObjectId("62a78b4b7960f45ec00b89be"),
"title" : "The Hobbit: An Unexpected Journey",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : "A reluctant hobbit, Bilbo Baggins, sets out to the Lonely Mountain with a spirited group of dwarves to reclaim their mountain home - and the gold within it - from the dragon Smaug."
}
{
"_id" : ObjectId("62a78b4b7960f45ec00b89c0"),
"title" : "The Hobbit: The Battle of the Five Armies",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : "Bilbo and Company are forced to engage in a war against an array of combatants and keep the the lonely mountains from falling into the hands of a rising darkness."
}
> db.movies.find({"$or":[{"synopsis":{"$regex":"dwarves"}},{"synopsis":{"$regex":"hobbit"}}]}).pretty();
{
"_id" : ObjectId("62a78b4b7960f45ec00b89be"),
"title" : "The Hobbit: An Unexpected Journey",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : "A reluctant hobbit, Bilbo Baggins, sets out to the Lonely Mountain with a spirited group of dwarves to reclaim their mountain home - and the gold within it - from the dragon Smaug."
}
{
"_id" : ObjectId("62a78b4b7960f45ec00b89bf"),
"title" : "The Hobbit: The Desolation of smug",
"writer" : "J.R.R. Tolkein",
"year" : 2013,
"franchise" : "The Hobbit",
"synopsis" : "The dwarves, along with Bilbo Baggins and Gandalf the Grey, continue their quest to reclaim Erebor, their homeland, from Smaug. Bilbo Baggins is in possession of a mysterious and magical ring."
}
> db.movies.find({"$and":[{"synopsis":{"$regex":"gold"}},{"synopsis":{"$regex":"dragon"}}]}).pretty();
{
"_id" : ObjectId("62a78b4b7960f45ec00b89be"),
"title" : "The Hobbit: An Unexpected Journey",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : "A reluctant hobbit, Bilbo Baggins, sets out to the Lonely Mountain with a spirited group of dwarves to reclaim their mountain home - and the gold within it - from the dragon Smaug."
}Delete Documents> db.movies.deleteOne({"title":"Pee Wee Herman's Big Adventure"});
{ "acknowledged" : true, "deletedCount" : 1 }
> db.movies.deleteOne({"title":"Avatar"});
{ "acknowledged" : true, "deletedCount" : 1 }
> db.movies.find({},{_id:0,title:1}).pretty();
{ "title" : "Fight Club" }
{ "title" : "Pulp Fiction" }
{ "title" : "Inglorius Basterds" }
{ "title" : "The Hobbit: An Unexpected Journey" }
{ "title" : "The Hobbit: The Desolation of smug" }
{ "title" : "The Hobbit: The Battle of the Five Armies" }RelationshipsInsert the following documents into a users collection> db.users.insertMany([{"username":"GoodGuyGreg","first_name":"Good Guy", "last_name" : "Greg"},{ "username" : "ScumbagSteve", "full_name" : {"first" : "Scumbag" , "last" : "Steve"}}]);
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("62b2127458e2771bd28580eb"),
ObjectId("62b2127458e2771bd28580ec")
]
}Insert the following documents into a posts collection> db.posts.insertMany([{"username":"GoodGuyGreg","title" :" Passes out at party", "body" :" Wakes up early and cleans house"},{"username":"GoodGuyGreg","title" :"Steals your identity", "body" :"Raises your credit score"},{"username":"GoodGuyGreg","title" :" Reports a bug in your code", "body" :" Sends you a Pull Request"},{"username":"ScumbagSteve","title" :" Borrows something", "body" :"Sells it"},{"username":"ScumbagSteve","title" :"Borrows everything", "body" :"The end"},{"username":"ScumbagSteve","title" :"Forks your repo on github", "body" :"Sets to private"}]);
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("62b2131f58e2771bd28580ed"),
ObjectId("62b2131f58e2771bd28580ee"),
ObjectId("62b2131f58e2771bd28580ef"),
ObjectId("62b2131f58e2771bd28580f0"),
ObjectId("62b2131f58e2771bd28580f1"),
ObjectId("62b2131f58e2771bd28580f2")
]
}Insert the following documents into a comments collection> db.comments.insertMany([
... {
... username: "GoodGuyGreg",
... comment: "Hope you got a good deal!",
... post: ObjectId("62b2131f58e2771bd28580f0")
... },
... {
... username: "GoodGuyGreg",
... comment: "Don't violate the licensing agreement!",
... post: ObjectId("62b2131f58e2771bd28580f1")
... },
... {
... username: "GoodGuyGreg",
... comment: "Don't violate the licensing agreement!",
... post: ObjectId("62b2131f58e2771bd28580f2")
... },
... {
... username: "ScumbagSteve",
... comment: "It still isn't clean",
... post: ObjectId("62b2131f58e2771bd28580ed")
... },
... {
... username: "ScumbagSteve",
... comment: "Denied your PR cause I found a hack",
... post: ObjectId("62b2131f58e2771bd28580ef")
... }
... ]);
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("62b2164158e2771bd28580f3"),
ObjectId("62b2164158e2771bd28580f4"),
ObjectId("62b2164158e2771bd28580f5"),
ObjectId("62b2164158e2771bd28580f6"),
ObjectId("62b2164158e2771bd28580f7")
]
}Querying related collections> db.users.find().pretty();
{
"_id" : ObjectId("62b2127458e2771bd28580eb"),
"username" : "GoodGuyGreg",
"first_name" : "Good Guy",
"last_name" : "Greg"
}
{
"_id" : ObjectId("62b2127458e2771bd28580ec"),
"username" : "ScumbagSteve",
"full_name" : {
"first" : "Scumbag",
"last" : "Steve"
}
}
> db.posts.find().pretty();
{
"_id" : ObjectId("62b2131f58e2771bd28580ed"),
"username" : "GoodGuyGreg",
"title" : " Passes out at party",
"body" : " Wakes up early and cleans house"
}
{
"_id" : ObjectId("62b2131f58e2771bd28580ee"),
"username" : "GoodGuyGreg",
"title" : "Steals your identity",
"body" : "Raises your credit score"
}
{
"_id" : ObjectId("62b2131f58e2771bd28580ef"),
"username" : "GoodGuyGreg",
"title" : " Reports a bug in your code",
"body" : " Sends you a Pull Request"
}
{
"_id" : ObjectId("62b2131f58e2771bd28580f0"),
"username" : "ScumbagSteve",
"title" : " Borrows something",
"body" : "Sells it"
}
{
"_id" : ObjectId("62b2131f58e2771bd28580f1"),
"username" : "ScumbagSteve",
"title" : "Borrows everything",
"body" : "The end"
}
{
"_id" : ObjectId("62b2131f58e2771bd28580f2"),
"username" : "ScumbagSteve",
"title" : "Forks your repo on github",
"body" : "Sets to private"
}
> db.posts.find({ username: 'GoodGuyGreg' }).pretty();
{
"_id" : ObjectId("62b2131f58e2771bd28580ed"),
"username" : "GoodGuyGreg",
"title" : " Passes out at party",
"body" : " Wakes up early and cleans house"
}
{
"_id" : ObjectId("62b2131f58e2771bd28580ee"),
"username" : "GoodGuyGreg",
"title" : "Steals your identity",
"body" : "Raises your credit score"
}
{
"_id" : ObjectId("62b2131f58e2771bd28580ef"),
"username" : "GoodGuyGreg",
"title" : " Reports a bug in your code",
"body" : " Sends you a Pull Request"
}
> db.posts.find({ username: 'ScumbagSteve' }).pretty();
{
"_id" : ObjectId("62b2131f58e2771bd28580f0"),
"username" : "ScumbagSteve",
"title" : " Borrows something",
"body" : "Sells it"
}
{
"_id" : ObjectId("62b2131f58e2771bd28580f1"),
"username" : "ScumbagSteve",
"title" : "Borrows everything",
"body" : "The end"
}
{
"_id" : ObjectId("62b2131f58e2771bd28580f2"),
"username" : "ScumbagSteve",
"title" : "Forks your repo on github",
"body" : "Sets to private"
}
> db.comments.find().pretty();
{
"_id" : ObjectId("62b2164158e2771bd28580f3"),
"username" : "GoodGuyGreg",
"comment" : "Hope you got a good deal!",
"post" : ObjectId("62b2131f58e2771bd28580f0")
}
{
"_id" : ObjectId("62b2164158e2771bd28580f4"),
"username" : "GoodGuyGreg",
"comment" : "Don't violate the licensing agreement!",
"post" : ObjectId("62b2131f58e2771bd28580f1")
}
{
"_id" : ObjectId("62b2164158e2771bd28580f5"),
"username" : "GoodGuyGreg",
"comment" : "Don't violate the licensing agreement!",
"post" : ObjectId("62b2131f58e2771bd28580f2")
}
{
"_id" : ObjectId("62b2164158e2771bd28580f6"),
"username" : "ScumbagSteve",
"comment" : "It still isn't clean",
"post" : ObjectId("62b2131f58e2771bd28580ed")
}
{
"_id" : ObjectId("62b2164158e2771bd28580f7"),
"username" : "ScumbagSteve",
"comment" : "Denied your PR cause I found a hack",
"post" : ObjectId("62b2131f58e2771bd28580ef")
}
> db.comments.find({ username: 'GoodGuyGreg' }).pretty();
{
"_id" : ObjectId("62b2164158e2771bd28580f3"),
"username" : "GoodGuyGreg",
"comment" : "Hope you got a good deal!",
"post" : ObjectId("62b2131f58e2771bd28580f0")
}
{
"_id" : ObjectId("62b2164158e2771bd28580f4"),
"username" : "GoodGuyGreg",
"comment" : "Don't violate the licensing agreement!",
"post" : ObjectId("62b2131f58e2771bd28580f1")
}
{
"_id" : ObjectId("62b2164158e2771bd28580f5"),
"username" : "GoodGuyGreg",
"comment" : "Don't violate the licensing agreement!",
"post" : ObjectId("62b2131f58e2771bd28580f2")
}
> db.comments.find({ username: 'ScumbagSteve' }).pretty();
{
"_id" : ObjectId("62b2164158e2771bd28580f6"),
"username" : "ScumbagSteve",
"comment" : "It still isn't clean",
"post" : ObjectId("62b2131f58e2771bd28580ed")
}
{
"_id" : ObjectId("62b2164158e2771bd28580f7"),
"username" : "ScumbagSteve",
"comment" : "Denied your PR cause I found a hack",
"post" : ObjectId("62b2131f58e2771bd28580ef")
} |
> use mongo_practice
switched to db mongo_practice
> db.movies.insertMany([{
... title: "Fight Club", writer: "Chuck Palahniuk", year: 1999, actors: ["Brad Pitt", "EEdward Norton"]},
... {title: "Pulp Fiction", writer: "Quentin Tarantino", year: 1994, actors: ["John Travolta", "Uma Thurman"]},
... {title: "Inglorius Basterds", writer: "Quentin Tarantino", year: 2009, actors: ["Brad Pitt", "Diane Kruger", "Eli Roth"]},
... {title: "The Hobbit: An Unexpected Journey", writer: "J.R.R. Tolkein", year: 2012, franchise: "The Hobbit"},
... {title: "The Hobbit: The Desolation of smug", writer: "J.R.R. Tolkein", year: 2013, franchise: "The Hobbit"},
... {title: "The Hobbit: The Battle of the Five Armies", writer: "J.R.R. Tolkein", year: 2012, franchise: "The Hobbit",
... synopsis: "Bilbo and Company are forced to engage in a war against an array of combatants and keep the the lonely mountains from falling into the hands of a rising darkness."},
... {title: "Pee Wee Herman's Big Adventure"}, {title: "Avatar"}])
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("62a81d49a0464c977fa8f2e2"),
ObjectId("62a81d49a0464c977fa8f2e3"),
ObjectId("62a81d49a0464c977fa8f2e4"),
ObjectId("62a81d49a0464c977fa8f2e5"),
ObjectId("62a81d49a0464c977fa8f2e6"),
ObjectId("62a81d49a0464c977fa8f2e7"),
ObjectId("62a81d49a0464c977fa8f2e8"),
ObjectId("62a81d49a0464c977fa8f2e9")
]
}
> db.movies.find().pretty()
{
"_id" : ObjectId("62a81d49a0464c977fa8f2e2"),
"title" : "Fight Club",
"writer" : "Chuck Palahniuk",
"year" : 1999,
"actors" : [
"Brad Pitt",
"EEdward Norton"
]
}
{
"_id" : ObjectId("62a81d49a0464c977fa8f2e3"),
"title" : "Pulp Fiction",
"writer" : "Quentin Tarantino",
"year" : 1994,
"actors" : [
"John Travolta",
"Uma Thurman"
]
}
{
"_id" : ObjectId("62a81d49a0464c977fa8f2e4"),
"title" : "Inglorius Basterds",
"writer" : "Quentin Tarantino",
"year" : 2009,
"actors" : [
"Brad Pitt",
"Diane Kruger",
"Eli Roth"
]
}
{
"_id" : ObjectId("62a81d49a0464c977fa8f2e5"),
"title" : "The Hobbit: An Unexpected Journey",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit"
}
{
"_id" : ObjectId("62a81d49a0464c977fa8f2e6"),
"title" : "The Hobbit: The Desolation of smug",
"writer" : "J.R.R. Tolkein",
"year" : 2013,
"franchise" : "The Hobbit"
}
{
"_id" : ObjectId("62a81d49a0464c977fa8f2e7"),
"title" : "The Hobbit: The Battle of the Five Armies",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : "Bilbo and Company are forced to engage in a war against an array of combatants and keep the the lonely mountains from falling into the hands of a rising darkness."
}
{
"_id" : ObjectId("62a81d49a0464c977fa8f2e8"),
"title" : "Pee Wee Herman's Big Adventure"
}
{ "_id" : ObjectId("62a81d49a0464c977fa8f2e9"), "title" : "Avatar" }
> db.movies.find({writer:"Quentin Tarantino"}).pretty()
{
"_id" : ObjectId("62a81d49a0464c977fa8f2e3"),
"title" : "Pulp Fiction",
"writer" : "Quentin Tarantino",
"year" : 1994,
"actors" : [
"John Travolta",
"Uma Thurman"
]
}
{
"_id" : ObjectId("62a81d49a0464c977fa8f2e4"),
"title" : "Inglorius Basterds",
"writer" : "Quentin Tarantino",
"year" : 2009,
"actors" : [
"Brad Pitt",
"Diane Kruger",
"Eli Roth"
]
}
> db.movies.find({"actors":{"$in":["Brad Pitt"]}}).pretty()
{
"_id" : ObjectId("62a81d49a0464c977fa8f2e2"),
"title" : "Fight Club",
"writer" : "Chuck Palahniuk",
"year" : 1999,
"actors" : [
"Brad Pitt",
"EEdward Norton"
]
}
{
"_id" : ObjectId("62a81d49a0464c977fa8f2e4"),
"title" : "Inglorius Basterds",
"writer" : "Quentin Tarantino",
"year" : 2009,
"actors" : [
"Brad Pitt",
"Diane Kruger",
"Eli Roth"
]
}
> db.movies.find({"franchise":"The Hobbit"}).pretty()
{
"_id" : ObjectId("62a81d49a0464c977fa8f2e5"),
"title" : "The Hobbit: An Unexpected Journey",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit"
}
{
"_id" : ObjectId("62a81d49a0464c977fa8f2e6"),
"title" : "The Hobbit: The Desolation of smug",
"writer" : "J.R.R. Tolkein",
"year" : 2013,
"franchise" : "The Hobbit"
}
{
"_id" : ObjectId("62a81d49a0464c977fa8f2e7"),
"title" : "The Hobbit: The Battle of the Five Armies",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : "Bilbo and Company are forced to engage in a war against an array of combatants and keep the the lonely mountains from falling into the hands of a rising darkness."
}
> db.movies.find({$and:[{"year":{"$gte":1990}},{"year":{"$lte":1999}}]}).pretty()
{
"_id" : ObjectId("62a81d49a0464c977fa8f2e2"),
"title" : "Fight Club",
"writer" : "Chuck Palahniuk",
"year" : 1999,
"actors" : [
"Brad Pitt",
"EEdward Norton"
]
}
{
"_id" : ObjectId("62a81d49a0464c977fa8f2e3"),
"title" : "Pulp Fiction",
"writer" : "Quentin Tarantino",
"year" : 1994,
"actors" : [
"John Travolta",
"Uma Thurman"
]
}
> db.movies.find({$or:[{"year":{"$gt":2010}},{"year":{"$lt":2000}}]}).pretty()
{
"_id" : ObjectId("62a81d49a0464c977fa8f2e2"),
"title" : "Fight Club",
"writer" : "Chuck Palahniuk",
"year" : 1999,
"actors" : [
"Brad Pitt",
"EEdward Norton"
]
}
{
"_id" : ObjectId("62a81d49a0464c977fa8f2e3"),
"title" : "Pulp Fiction",
"writer" : "Quentin Tarantino",
"year" : 1994,
"actors" : [
"John Travolta",
"Uma Thurman"
]
}
{
"_id" : ObjectId("62a81d49a0464c977fa8f2e5"),
"title" : "The Hobbit: An Unexpected Journey",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit"
}
{
"_id" : ObjectId("62a81d49a0464c977fa8f2e6"),
"title" : "The Hobbit: The Desolation of smug",
"writer" : "J.R.R. Tolkein",
"year" : 2013,
"franchise" : "The Hobbit"
}
{
"_id" : ObjectId("62a81d49a0464c977fa8f2e7"),
"title" : "The Hobbit: The Battle of the Five Armies",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : "Bilbo and Company are forced to engage in a war against an array of combatants and keep the the lonely mountains from falling into the hands of a rising darkness."
}
> db.movies.updateOne({"title":"The Hobbit: An Unexpected Journey"},{$set:{"synopsis":"A reluctant hobbit, Bilbo Baggins, sets out to the Lonely Mountain with a spirited group of dwarves to reclaim their mountain home - and the gold within it - from the dragon Smaug."}})
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
> db.movies.updateOne({"title":"The Hobbit: The Desolation of Smaug"},{$set:{"synopsis":"The dwarves, along with Bilbo Baggins and Gandalf the Grey, continue their quest to reclaim Erebor, their homeland, from Smaug. Bilbo Baggins is in possession of a mysterious and magical ring."}})
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
> db.movies.updateOne({"title":"Pulp Fiction"},{$push:{"actors":"Samuel L. Jackson"}})
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
> db.movies.find({"title":"Pulp Fiction"},{_id:0,title:1,actors:1})
{ "title" : "Pulp Fiction", "actors" : [ "John Travolta", "Uma Thurman", "Samuel L. Jackson" ] }
> db.movies.find({"synopsis":{"$regex":"Bilbo"}}).pretty()
{
"_id" : ObjectId("62a81d49a0464c977fa8f2e5"),
"title" : "The Hobbit: An Unexpected Journey",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : "A reluctant hobbit, Bilbo Baggins, sets out to the Lonely Mountain with a spirited group of dwarves to reclaim their mountain home - and the gold within it - from the dragon Smaug."
}
{
"_id" : ObjectId("62a81d49a0464c977fa8f2e6"),
"title" : "The Hobbit: The Desolation of Smaug",
"writer" : "J.R.R. Tolkein",
"year" : 2013,
"franchise" : "The Hobbit",
"synopsis" : "The dwarves, along with Bilbo Baggins and Gandalf the Grey, continue their quest to reclaim Erebor, their homeland, from Smaug. Bilbo Baggins is in possession of a mysterious and magical ring."
}
{
"_id" : ObjectId("62a81d49a0464c977fa8f2e7"),
"title" : "The Hobbit: The Battle of the Five Armies",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : "Bilbo and Company are forced to engage in a war against an array of combatants and keep the the lonely mountains from falling into the hands of a rising darkness."
}
> db.movies.find({"synopsis":{"$regex":"Gandalf"}}).pretty()
{
"_id" : ObjectId("62a81d49a0464c977fa8f2e6"),
"title" : "The Hobbit: The Desolation of Smaug",
"writer" : "J.R.R. Tolkein",
"year" : 2013,
"franchise" : "The Hobbit",
"synopsis" : "The dwarves, along with Bilbo Baggins and Gandalf the Grey, continue their quest to reclaim Erebor, their homeland, from Smaug. Bilbo Baggins is in possession of a mysterious and magical ring."
}
> db.movies.find({"synopsis":{"$regex":"Bilbo",$not:{"$regex":"Gandalf"}}}).pretty()
{
"_id" : ObjectId("62a81d49a0464c977fa8f2e5"),
"title" : "The Hobbit: An Unexpected Journey",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : "A reluctant hobbit, Bilbo Baggins, sets out to the Lonely Mountain with a spirited group of dwarves to reclaim their mountain home - and the gold within it - from the dragon Smaug."
}
{
"_id" : ObjectId("62a81d49a0464c977fa8f2e7"),
"title" : "The Hobbit: The Battle of the Five Armies",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : "Bilbo and Company are forced to engage in a war against an array of combatants and keep the the lonely mountains from falling into the hands of a rising darkness."
}
> db.movies.find({"$or":[{"synopsis":{"$regex":"dwarves"}},{"synopsis":{"$regex":"hobbit"}}]}).pretty()
{
"_id" : ObjectId("62a81d49a0464c977fa8f2e5"),
"title" : "The Hobbit: An Unexpected Journey",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : "A reluctant hobbit, Bilbo Baggins, sets out to the Lonely Mountain with a spirited group of dwarves to reclaim their mountain home - and the gold within it - from the dragon Smaug."
}
{
"_id" : ObjectId("62a81d49a0464c977fa8f2e6"),
"title" : "The Hobbit: The Desolation of Smaug",
"writer" : "J.R.R. Tolkein",
"year" : 2013,
"franchise" : "The Hobbit",
"synopsis" : "The dwarves, along with Bilbo Baggins and Gandalf the Grey, continue their quest to reclaim Erebor, their homeland, from Smaug. Bilbo Baggins is in possession of a mysterious and magical ring."
}
> db.movies.find({"$and":[{"synopsis":{"$regex":"gold"}},{"synopsis":{"$regex":"dragon"}}]}).pretty()
{
"_id" : ObjectId("62a81d49a0464c977fa8f2e5"),
"title" : "The Hobbit: An Unexpected Journey",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : "A reluctant hobbit, Bilbo Baggins, sets out to the Lonely Mountain with a spirited group of dwarves to reclaim their mountain home - and the gold within it - from the dragon Smaug."
}
> db.movies.deleteOne({"title":"Pee Wee Herman's Big Adventure"})
{ "acknowledged" : true, "deletedCount" : 1 }
> db.movies.find({},{_id:0,title:1}).pretty()
{ "title" : "Fight Club" }
{ "title" : "Pulp Fiction" }
{ "title" : "Inglorius Basterds" }
{ "title" : "The Hobbit: An Unexpected Journey" }
{ "title" : "The Hobbit: The Desolation of Smaug" }
{ "title" : "The Hobbit: The Battle of the Five Armies" }
{ "title" : "Avatar" }
> db.movies.deleteOne({"title":"Avatar"})
{ "acknowledged" : true, "deletedCount" : 1 }
> db.movies.find({},{_id:0,title:1}).pretty()
{ "title" : "Fight Club" }
{ "title" : "Pulp Fiction" }
{ "title" : "Inglorius Basterds" }
{ "title" : "The Hobbit: An Unexpected Journey" }
{ "title" : "The Hobbit: The Desolation of Smaug" }
{ "title" : "The Hobbit: The Battle of the Five Armies" }
> db.users.insertMany([{"username":"GoodGuyGreg","first_name":"Good Guy", "last_name" : "Greg"},{ "username" : "ScumbagSteve", "full_name" : {"first" : "Scumbag" , "last" : "Steve"}}])
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("62aed6f72965e0d6d04e8020"),
ObjectId("62aed6f72965e0d6d04e8021")
]
}
> db.users.find().pretty()
{
"_id" : ObjectId("62aed6f72965e0d6d04e8020"),
"username" : "GoodGuyGreg",
"first_name" : "Good Guy",
"last_name" : "Greg"
}
{
"_id" : ObjectId("62aed6f72965e0d6d04e8021"),
"username" : "ScumbagSteve",
"full_name" : {
"first" : "Scumbag",
"last" : "Steve"
}
}
> db.posts.insertMany([{"username":"GoodGuyGreg","title" :" Passes out at party", "body" :" Wakes up early and cleans house"},{"username":"GoodGuyGreg","title" :"Steals your identity", "body" :"Raises your credit score"},{"username":"GoodGuyGreg","title" :" Reports a bug in your code", "body" :" Sends you a Pull Request"},{"username":"ScumbagSteve","title" :" Borrows something", "body" :"Sells it"},{"username":"ScumbagSteve","title" :"Borrows everything", "body" :"The end"},{"username":"ScumbagSteve","title" :"Forks your repo on github", "body" :"Sets to private"}])
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("62aedb802965e0d6d04e8022"),
ObjectId("62aedb802965e0d6d04e8023"),
ObjectId("62aedb802965e0d6d04e8024"),
ObjectId("62aedb802965e0d6d04e8025"),
ObjectId("62aedb802965e0d6d04e8026"),
ObjectId("62aedb802965e0d6d04e8027")
]
}
> db.users.find().pretty()
{
"_id" : ObjectId("62aed6f72965e0d6d04e8020"),
"username" : "GoodGuyGreg",
"first_name" : "Good Guy",
"last_name" : "Greg"
}
{
"_id" : ObjectId("62aed6f72965e0d6d04e8021"),
"username" : "ScumbagSteve",
"full_name" : {
"first" : "Scumbag",
"last" : "Steve"
}
}
> db.posts.find().pretty()
{
"_id" : ObjectId("62aedb802965e0d6d04e8022"),
"username" : "GoodGuyGreg",
"title" : " Passes out at party",
"body" : " Wakes up early and cleans house"
}
{
"_id" : ObjectId("62aedb802965e0d6d04e8023"),
"username" : "GoodGuyGreg",
"title" : "Steals your identity",
"body" : "Raises your credit score"
}
{
"_id" : ObjectId("62aedb802965e0d6d04e8024"),
"username" : "GoodGuyGreg",
"title" : " Reports a bug in your code",
"body" : " Sends you a Pull Request"
}
{
"_id" : ObjectId("62aedb802965e0d6d04e8025"),
"username" : "ScumbagSteve",
"title" : " Borrows something",
"body" : "Sells it"
}
{
"_id" : ObjectId("62aedb802965e0d6d04e8026"),
"username" : "ScumbagSteve",
"title" : "Borrows everything",
"body" : "The end"
}
{
"_id" : ObjectId("62aedb802965e0d6d04e8027"),
"username" : "ScumbagSteve",
"title" : "Forks your repo on github",
"body" : "Sets to private"
}
> db.posts.find({username:"GoodGuyGreg"}).pretty()
{
"_id" : ObjectId("62aedb802965e0d6d04e8022"),
"username" : "GoodGuyGreg",
"title" : " Passes out at party",
"body" : " Wakes up early and cleans house"
}
{
"_id" : ObjectId("62aedb802965e0d6d04e8023"),
"username" : "GoodGuyGreg",
"title" : "Steals your identity",
"body" : "Raises your credit score"
}
{
"_id" : ObjectId("62aedb802965e0d6d04e8024"),
"username" : "GoodGuyGreg",
"title" : " Reports a bug in your code",
"body" : " Sends you a Pull Request"
}
> db.posts.find({username:"ScumbagSteve"}).pretty()
{
"_id" : ObjectId("62aedb802965e0d6d04e8025"),
"username" : "ScumbagSteve",
"title" : " Borrows something",
"body" : "Sells it"
}
{
"_id" : ObjectId("62aedb802965e0d6d04e8026"),
"username" : "ScumbagSteve",
"title" : "Borrows everything",
"body" : "The end"
}
{
"_id" : ObjectId("62aedb802965e0d6d04e8027"),
"username" : "ScumbagSteve",
"title" : "Forks your repo on github",
"body" : "Sets to private"
} |
> show dbs
Employees 0.000GB
admin 0.000GB
config 0.000GB
local 0.000GB
testDb 0.000GB
> use mongo_practice
switched to db mongo_practice
> db.movies.insertMany([{title: "Fight Club", writer: "Chuck Palahniuk", year: 1999, actors: ["Brad Pitt", "Edb.movies.insertMany([{title: "Fight Club", writer: "Chuck Palahniuk", year: 1999, actors: ["Brad Pitt", "EEdward Norton"]},
... {title: "Pulp Fiction", writer: "Quentin Tarantino", year: 1994, actors: ["John Travolta", "Uma Thurman"]},
... {title: "Inglorius Basterds", writer: "Quentin Tarantino", year: 2009, actors: ["Brad Pitt", "Diane Kruger", "Eli Roth"]},
... {title: "The Hobbit: An Unexpected Journey", writer: "J.R.R. Tolkein", year: 2012, franchise: "The Hobbit"},
... {title: "The Hobbit: The Desolation of smug", writer: "J.R.R. Tolkein", year: 2013, franchise: "The Hobbit"},
... {title: "The Hobbit: The Battle of the Five Armies", writer: "J.R.R. Tolkein", year: 2012, franchise: "The Hobbit",
... synopsis: "Bilbo and Company are forced to engage in a war against an array of combatants and keep the the lonely mountains from falling into the hands of a rising darkness."},
... {title: "Pee Wee Herman's Big Adventure"}, {title: "Avatar"}]);
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("62a8928f6d6be812cfae97d0"),
ObjectId("62a8928f6d6be812cfae97d1"),
ObjectId("62a8928f6d6be812cfae97d2"),
ObjectId("62a8928f6d6be812cfae97d3"),
ObjectId("62a8928f6d6be812cfae97d4"),
ObjectId("62a8928f6d6be812cfae97d5"),
ObjectId("62a8928f6d6be812cfae97d6"),
ObjectId("62a8928f6d6be812cfae97d7")
]
}
> db.movies.find({}).pretty();
{
"_id" : ObjectId("62a8928f6d6be812cfae97d0"),
"title" : "Fight Club",
"writer" : "Chuck Palahniuk",
"year" : 1999,
"actors" : [
"Brad Pitt",
"EEdward Norton"
]
}
{
"_id" : ObjectId("62a8928f6d6be812cfae97d1"),
"title" : "Pulp Fiction",
"writer" : "Quentin Tarantino",
"year" : 1994,
"actors" : [
"John Travolta",
"Uma Thurman"
]
}
{
"_id" : ObjectId("62a8928f6d6be812cfae97d2"),
"title" : "Inglorius Basterds",
"writer" : "Quentin Tarantino",
"year" : 2009,
"actors" : [
"Brad Pitt",
"Diane Kruger",
"Eli Roth"
]
}
{
"_id" : ObjectId("62a8928f6d6be812cfae97d3"),
"title" : "The Hobbit: An Unexpected Journey",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit"
}
{
"_id" : ObjectId("62a8928f6d6be812cfae97d4"),
"title" : "The Hobbit: The Desolation of smug",
"writer" : "J.R.R. Tolkein",
"year" : 2013,
"franchise" : "The Hobbit"
}
{
"_id" : ObjectId("62a8928f6d6be812cfae97d5"),
"title" : "The Hobbit: The Battle of the Five Armies",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : "Bilbo and Company are forced to engage in a war against an array of combatants and keep the the lonely mountains from falling into the hands of a rising darkness."
}
{
"_id" : ObjectId("62a8928f6d6be812cfae97d6"),
"title" : "Pee Wee Herman's Big Adventure"
}
{ "_id" : ObjectId("62a8928f6d6be812cfae97d7"), "title" : "Avatar" }
>db.movies.find({writer:"Quentin Tarantino"}).pretty();
{
"_id" : ObjectId("62a8928f6d6be812cfae97d1"),
"title" : "Pulp Fiction",
"writer" : "Quentin Tarantino",
"year" : 1994,
"actors" : [
"John Travolta",
"Uma Thurman"
]
}
{
"_id" : ObjectId("62a8928f6d6be812cfae97d2"),
"title" : "Inglorius Basterds",
"writer" : "Quentin Tarantino",
"year" : 2009,
"actors" : [
"Brad Pitt",
"Diane Kruger",
"Eli Roth"
]
}
> db.movies.find({actors:"Brad Pitt"}).pretty();
{
"_id" : ObjectId("62a8928f6d6be812cfae97d0"),
"title" : "Fight Club",
"writer" : "Chuck Palahniuk",
"year" : 1999,
"actors" : [
"Brad Pitt",
"EEdward Norton"
]
}
{
"_id" : ObjectId("62a8928f6d6be812cfae97d2"),
"title" : "Inglorius Basterds",
"writer" : "Quentin Tarantino",
"year" : 2009,
"actors" : [
"Brad Pitt",
"Diane Kruger",
"Eli Roth"
]
}
> db.movies.find({franchise:"The Hobbit"}).pretty();
{
"_id" : ObjectId("62a8928f6d6be812cfae97d3"),
"title" : "The Hobbit: An Unexpected Journey",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit"
}
{
"_id" : ObjectId("62a8928f6d6be812cfae97d4"),
"title" : "The Hobbit: The Desolation of smug",
"writer" : "J.R.R. Tolkein",
"year" : 2013,
"franchise" : "The Hobbit"
}
{
"_id" : ObjectId("62a8928f6d6be812cfae97d5"),
"title" : "The Hobbit: The Battle of the Five Armies",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : "Bilbo and Company are forced to engage in a war against an array of combatants and keep the the lonely mountains from falling into the hands of a rising darkness."
}
> db.movies.find({$or :[{year: {$lt:2000}},{year: {$gt:2010}} ] }).pretty();
{
"_id" : ObjectId("62a8928f6d6be812cfae97d0"),
"title" : "Fight Club",
"writer" : "Chuck Palahniuk",
"year" : 1999,
"actors" : [
"Brad Pitt",
"EEdward Norton"
]
}
{
"_id" : ObjectId("62a8928f6d6be812cfae97d1"),
"title" : "Pulp Fiction",
"writer" : "Quentin Tarantino",
"year" : 1994,
"actors" : [
"John Travolta",
"Uma Thurman"
]
}
{
"_id" : ObjectId("62a8928f6d6be812cfae97d3"),
"title" : "The Hobbit: An Unexpected Journey",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit"
}
{
"_id" : ObjectId("62a8928f6d6be812cfae97d4"),
"title" : "The Hobbit: The Desolation of smug",
"writer" : "J.R.R. Tolkein",
"year" : 2013,
"franchise" : "The Hobbit"
}
{
"_id" : ObjectId("62a8928f6d6be812cfae97d5"),
"title" : "The Hobbit: The Battle of the Five Armies",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : "Bilbo and Company are forced to engage in a war against an array of combatants and keep the the lonely mountains from falling into the hands of a rising darkness."
}
> db.movies.updateOne({title:"The Hobbit: An Unexpected Journey"},{$set:{synopsis:"A reluctant hobbit, Bilbo Baggins, sets out to the Lonely Mountain with a spirited group of dwarves to reclaim their mountain home - and the gold within it - from the dragon Smaug."}})
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
> db.movies.updateOne({title:"The Hobbit: The Desolation of smug"},{$set:{synopsis:"The dwarves, along with Bilbo Baggins and Gandalf the Grey, continue their quest to reclaim Erebor, their homeland, from Smaug. Bilbo Baggins is in possession of a mysterious and magical ring."}})
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
>db.movies.updateOne({title:"Pulp Fiction"},{$push:{actor:"Samuel L. Jackson"}})
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
> db.movies.find({"synopsis":{"$regex":"Bilbo"}}).pretty()
{
"_id" : ObjectId("62a8928f6d6be812cfae97d3"),
"title" : "The Hobbit: An Unexpected Journey",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : "A reluctant hobbit, Bilbo Baggins, sets out to the Lonely Mountain with a spirited group of dwarves to reclaim their mountain home - and the gold within it - from the dragon Smaug."
}
{
"_id" : ObjectId("62a8928f6d6be812cfae97d4"),
"title" : "The Hobbit: The Desolation of smug",
"writer" : "J.R.R. Tolkein",
"year" : 2013,
"franchise" : "The Hobbit",
"synopsis" : "The dwarves, along with Bilbo Baggins and Gandalf the Grey, continue their quest to reclaim Erebor, their homeland, from Smaug. Bilbo Baggins is in possession of a mysterious and magical ring."
}
{
"_id" : ObjectId("62a8928f6d6be812cfae97d5"),
"title" : "The Hobbit: The Battle of the Five Armies",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : "Bilbo and Company are forced to engage in a war against an array of combatants and keep the the lonely mountains from falling into the hands of a rising darkness."
}
>db.movies.find({"synopsis":{"$regex":"Gandalf"}}).pretty()
{
"_id" : ObjectId("62a8928f6d6be812cfae97d4"),
"title" : "The Hobbit: The Desolation of smug",
"writer" : "J.R.R. Tolkein",
"year" : 2013,
"franchise" : "The Hobbit",
"synopsis" : "The dwarves, along with Bilbo Baggins and Gandalf the Grey, continue their quest to reclaim Erebor, their homeland, from Smaug. Bilbo Baggins is in possession of a mysterious and magical ring."
}
> db.movies.find({"synopsis":{"$regex":"Bilbo",$not:{"$regex":"Gandalf"}}}).pretty()
{
"_id" : ObjectId("62a8928f6d6be812cfae97d3"),
"title" : "The Hobbit: An Unexpected Journey",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : "A reluctant hobbit, Bilbo Baggins, sets out to the Lonely Mountain with a spirited group of dwarves to reclaim their mountain home - and the gold within it - from the dragon Smaug."
}
{
"_id" : ObjectId("62a8928f6d6be812cfae97d5"),
"title" : "The Hobbit: The Battle of the Five Armies",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : "Bilbo and Company are forced to engage in a war against an array of combatants and keep the the lonely mountains from falling into the hands of a rising darkness."
}
> db.movies.find({"$or":[{"synopsis":{"$regex":"dwarves"}},{"synopsis":{"$regex":"hobbit"}}]}).pretty()
{
"_id" : ObjectId("62a8928f6d6be812cfae97d3"),
"title" : "The Hobbit: An Unexpected Journey",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : "A reluctant hobbit, Bilbo Baggins, sets out to the Lonely Mountain with a spirited group of dwarves to reclaim their mountain home - and the gold within it - from the dragon Smaug."
}
{
"_id" : ObjectId("62a8928f6d6be812cfae97d4"),
"title" : "The Hobbit: The Desolation of smug",
"writer" : "J.R.R. Tolkein",
"year" : 2013,
"franchise" : "The Hobbit",
"synopsis" : "The dwarves, along with Bilbo Baggins and Gandalf the Grey, continue their quest to reclaim Erebor, their homeland, from Smaug. Bilbo Baggins is in possession of a mysterious and magical ring."
}
> db.movies.find({"$and":[{"synopsis":{"$regex":"gold"}},{"synopsis":{"$regex":"dragon"}}]}).pretty()
{
"_id" : ObjectId("62a8928f6d6be812cfae97d3"),
"title" : "The Hobbit: An Unexpected Journey",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : "A reluctant hobbit, Bilbo Baggins, sets out to the Lonely Mountain with a spirited group of dwarves to reclaim their mountain home - and the gold within it - from the dragon Smaug."
}
> db.movies.deleteOne({"title":"Pee Wee Herman's Big Adventure"})
{ "acknowledged" : true, "deletedCount" : 1 }
> db.movies.deleteOne({"title":"Avatar"})
{ "acknowledged" : true, "deletedCount" : 1 }
> db.movies.find({},{_id:0,title:1}).pretty()
{ "title" : "Fight Club" }
{ "title" : "Pulp Fiction" }
{ "title" : "Inglorius Basterds" }
{ "title" : "The Hobbit: An Unexpected Journey" }
{ "title" : "The Hobbit: The Desolation of smug" }
{ "title" : "The Hobbit: The Battle of the Five Armies" }
> db.users.insertMany([{"username":"GoodGuyGreg","first_name":"Good Guy", "last_name" : "Greg"},{ "username" : "ScumbagSteve", "full_name" : {"first" : "Scumbag" , "last" : "Steve"}}])
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("62b187a6601e3a7a8b62dcd3"),
ObjectId("62b187a6601e3a7a8b62dcd4")
]
}
> db.users.find().pretty();
{
"_id" : ObjectId("62b187a6601e3a7a8b62dcd3"),
"username" : "GoodGuyGreg",
"first_name" : "Good Guy",
"last_name" : "Greg"
}
{
"_id" : ObjectId("62b187a6601e3a7a8b62dcd4"),
"username" : "ScumbagSteve",
"full_name" : {
"first" : "Scumbag",
"last" : "Steve"
}
}
> db.posts.insertMany([{"username":"GoodGuyGreg","title" :" Passes out at party", "body" :" Wakes up early and cleans house"},{"username":"GoodGuyGreg","title" :"Steals your identity", "body" :"Raises your credit score"},{"username":"GoodGuyGreg","title" :" Reports a bug in your code", "body" :" Sends you a Pull Request"},{"username":"ScumbagSteve","title" :" Borrows something", "body" :"Sells it"},{"username":"ScumbagSteve","title" :"Borrows everything", "body" :"The end"},{"username":"ScumbagSteve","title" :"Forks your repo on github", "body" :"Sets to private"}])
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("62b18887601e3a7a8b62dcd5"),
ObjectId("62b18887601e3a7a8b62dcd6"),
ObjectId("62b18887601e3a7a8b62dcd7"),
ObjectId("62b18887601e3a7a8b62dcd8"),
ObjectId("62b18887601e3a7a8b62dcd9"),
ObjectId("62b18887601e3a7a8b62dcda")
]
}
> db.users.find().pretty();
{
"_id" : ObjectId("62b187a6601e3a7a8b62dcd3"),
"username" : "GoodGuyGreg",
"first_name" : "Good Guy",
"last_name" : "Greg"
}
{
"_id" : ObjectId("62b187a6601e3a7a8b62dcd4"),
"username" : "ScumbagSteve",
"full_name" : {
"first" : "Scumbag",
"last" : "Steve"
}
}
> db.posts.find().pretty();
{
"_id" : ObjectId("62b18887601e3a7a8b62dcd5"),
"username" : "GoodGuyGreg",
"title" : " Passes out at party",
"body" : " Wakes up early and cleans house"
}
{
"_id" : ObjectId("62b18887601e3a7a8b62dcd6"),
"username" : "GoodGuyGreg",
"title" : "Steals your identity",
"body" : "Raises your credit score"
}
{
"_id" : ObjectId("62b18887601e3a7a8b62dcd7"),
"username" : "GoodGuyGreg",
"title" : " Reports a bug in your code",
"body" : " Sends you a Pull Request"
}
{
"_id" : ObjectId("62b18887601e3a7a8b62dcd8"),
"username" : "ScumbagSteve",
"title" : " Borrows something",
"body" : "Sells it"
}
{
"_id" : ObjectId("62b18887601e3a7a8b62dcd9"),
"username" : "ScumbagSteve",
"title" : "Borrows everything",
"body" : "The end"
}
{
"_id" : ObjectId("62b18887601e3a7a8b62dcda"),
"username" : "ScumbagSteve",
"title" : "Forks your repo on github",
"body" : "Sets to private"
}
> db.posts.find({username:"GoodGuyGreg"}).pretty()
{
"_id" : ObjectId("62b18887601e3a7a8b62dcd5"),
"username" : "GoodGuyGreg",
"title" : " Passes out at party",
"body" : " Wakes up early and cleans house"
}
{
"_id" : ObjectId("62b18887601e3a7a8b62dcd6"),
"username" : "GoodGuyGreg",
"title" : "Steals your identity",
"body" : "Raises your credit score"
}
{
"_id" : ObjectId("62b18887601e3a7a8b62dcd7"),
"username" : "GoodGuyGreg",
"title" : " Reports a bug in your code",
"body" : " Sends you a Pull Request"
}
> db.posts.find({username:"ScumbagSteve"}).pretty()
{
"_id" : ObjectId("62b18887601e3a7a8b62dcd8"),
"username" : "ScumbagSteve",
"title" : " Borrows something",
"body" : "Sells it"
}
{
"_id" : ObjectId("62b18887601e3a7a8b62dcd9"),
"username" : "ScumbagSteve",
"title" : "Borrows everything",
"body" : "The end"
}
{
"_id" : ObjectId("62b18887601e3a7a8b62dcda"),
"username" : "ScumbagSteve",
"title" : "Forks your repo on github",
"body" : "Sets to private"
} |
|
PS C:\Users\Amar> mongo
MongoDB shell version v5.0.8
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("c15fb7a5-25a8-4fec-8e01-e214aa5fdd45") }
MongoDB server version: 5.0.8
================
Warning: the "mongo" shell has been superseded by "mongosh",
which delivers improved usability and compatibility.The "mongo" shell has been deprecated and will be removed in
an upcoming release.
For installation instructions, see
https://docs.mongodb.com/mongodb-shell/install/
================
---
The server generated these startup warnings when booting:
2022-05-26T19:16:35.525+05:30: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
---
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).
The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.
To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
testdb 0.000GB
> use mongo_practice
switched to db mongo_practice
> db.movies.insertMany([{title: "Fight Club", writer: "Chuck Palahniuk", year: 1999, actors: ["Brad Pitt", "EEdward Norton"]},
... {title: "Pulp Fiction", writer: "Quentin Tarantino", year: 1994, actors: ["John Travolta", "Uma Thurman"]},
... {title: "Inglorius Basterds", writer: "Quentin Tarantino", year: 2009, actors: ["Brad Pitt", "Diane Kruger", "Eli Roth"]},
... {title: "The Hobbit: An Unexpected Journey", writer: "J.R.R. Tolkein", year: 2012, franchise: "The Hobbit"},
... {title: "The Hobbit: The Desolation of smug", writer: "J.R.R. Tolkein", year: 2013, franchise: "The Hobbit"},
... {title: "The Hobbit: The Battle of the Five Armies", writer: "J.R.R. Tolkein", year: 2012, franchise: "The Hobbit",
... synopsis: "Bilbo and Company are forced to engage in a war against an array of combatants and keep the the lonely mountains from falling into the hands of a rising darkness."},
... {title: "Pee Wee Herman's Big Adventure"}, {title: "Avatar"}]);
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("62ab3c592b507cce0d965dde"),
ObjectId("62ab3c592b507cce0d965ddf"),
ObjectId("62ab3c592b507cce0d965de0"),
ObjectId("62ab3c592b507cce0d965de1"),
ObjectId("62ab3c592b507cce0d965de2"),
ObjectId("62ab3c592b507cce0d965de3"),
ObjectId("62ab3c592b507cce0d965de4"),
ObjectId("62ab3c592b507cce0d965de5")
]
}
> db.movies.find({}).pretty();
{
"_id" : ObjectId("62ab3c592b507cce0d965dde"),
"title" : "Fight Club",
"writer" : "Chuck Palahniuk",
"year" : 1999,
"actors" : [
"Brad Pitt",
"EEdward Norton"
]
}
{
"_id" : ObjectId("62ab3c592b507cce0d965ddf"),
"title" : "Pulp Fiction",
"writer" : "Quentin Tarantino",
"year" : 1994,
"actors" : [
"John Travolta",
"Uma Thurman"
]
}
{
"_id" : ObjectId("62ab3c592b507cce0d965de0"),
"title" : "Inglorius Basterds",
"writer" : "Quentin Tarantino",
"year" : 2009,
"actors" : [
"Brad Pitt",
"Diane Kruger",
"Eli Roth"
]
}
{
"_id" : ObjectId("62ab3c592b507cce0d965de1"),
"title" : "The Hobbit: An Unexpected Journey",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit"
}
{
"_id" : ObjectId("62ab3c592b507cce0d965de2"),
"title" : "The Hobbit: The Desolation of smug",
"writer" : "J.R.R. Tolkein",
"year" : 2013,
"franchise" : "The Hobbit"
}
{
"_id" : ObjectId("62ab3c592b507cce0d965de3"),
"title" : "The Hobbit: The Battle of the Five Armies",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : "Bilbo and Company are forced to engage in a war against an array of combatants and keep the the lonely mountains from falling into the hands of a rising darkness."
}
{
"_id" : ObjectId("62ab3c592b507cce0d965de4"),
"title" : "Pee Wee Herman's Big Adventure"
}
{ "_id" : ObjectId("62ab3c592b507cce0d965de5"), "title" : "Avatar" } |
PS C:\Users\Amar> mongo
MongoDB shell version v5.0.8
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("c15fb7a5-25a8-4fec-8e01-e214aa5fdd45") }
MongoDB server version: 5.0.8
================
Warning: the "mongo" shell has been superseded by "mongosh",
which delivers improved usability and compatibility.The "mongo" shell has been deprecated and will be removed in
an upcoming release.
For installation instructions, see
https://docs.mongodb.com/mongodb-shell/install/
================
---
The server generated these startup warnings when booting:
2022-05-26T19:16:35.525+05:30: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
---
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).
The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.
To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
testdb 0.000GB
> use mongo_practice
switched to db mongo_practice
> db.movies.insertMany([{title: "Fight Club", writer: "Chuck Palahniuk", year: 1999, actors: ["Brad Pitt", "EEdward Norton"]},
... {title: "Pulp Fiction", writer: "Quentin Tarantino", year: 1994, actors: ["John Travolta", "Uma Thurman"]},
... {title: "Inglorius Basterds", writer: "Quentin Tarantino", year: 2009, actors: ["Brad Pitt", "Diane Kruger", "Eli Roth"]},
... {title: "The Hobbit: An Unexpected Journey", writer: "J.R.R. Tolkein", year: 2012, franchise: "The Hobbit"},
... {title: "The Hobbit: The Desolation of smug", writer: "J.R.R. Tolkein", year: 2013, franchise: "The Hobbit"},
... {title: "The Hobbit: The Battle of the Five Armies", writer: "J.R.R. Tolkein", year: 2012, franchise: "The Hobbit",
... synopsis: "Bilbo and Company are forced to engage in a war against an array of combatants and keep the the lonely mountains from falling into the hands of a rising darkness."},
... {title: "Pee Wee Herman's Big Adventure"}, {title: "Avatar"}]);
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("62ab3c592b507cce0d965dde"),
ObjectId("62ab3c592b507cce0d965ddf"),
ObjectId("62ab3c592b507cce0d965de0"),
ObjectId("62ab3c592b507cce0d965de1"),
ObjectId("62ab3c592b507cce0d965de2"),
ObjectId("62ab3c592b507cce0d965de3"),
ObjectId("62ab3c592b507cce0d965de4"),
ObjectId("62ab3c592b507cce0d965de5")
]
}
> db.movies.find({}).pretty();
{
"_id" : ObjectId("62ab3c592b507cce0d965dde"),
"title" : "Fight Club",
"writer" : "Chuck Palahniuk",
"year" : 1999,
"actors" : [
"Brad Pitt",
"EEdward Norton"
]
}
{
"_id" : ObjectId("62ab3c592b507cce0d965ddf"),
"title" : "Pulp Fiction",
"writer" : "Quentin Tarantino",
"year" : 1994,
"actors" : [
"John Travolta",
"Uma Thurman"
]
}
{
"_id" : ObjectId("62ab3c592b507cce0d965de0"),
"title" : "Inglorius Basterds",
"writer" : "Quentin Tarantino",
"year" : 2009,
"actors" : [
"Brad Pitt",
"Diane Kruger",
"Eli Roth"
]
}
{
"_id" : ObjectId("62ab3c592b507cce0d965de1"),
"title" : "The Hobbit: An Unexpected Journey",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit"
}
{
"_id" : ObjectId("62ab3c592b507cce0d965de2"),
"title" : "The Hobbit: The Desolation of smug",
"writer" : "J.R.R. Tolkein",
"year" : 2013,
"franchise" : "The Hobbit"
}
{
"_id" : ObjectId("62ab3c592b507cce0d965de3"),
"title" : "The Hobbit: The Battle of the Five Armies",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : "Bilbo and Company are forced to engage in a war against an array of combatants and keep the the lonely mountains from falling into the hands of a rising darkness."
}
{
"_id" : ObjectId("62ab3c592b507cce0d965de4"),
"title" : "Pee Wee Herman's Big Adventure"
}
{ "_id" : ObjectId("62ab3c592b507cce0d965de5"), "title" : "Avatar" } |
show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
mongo_practice 0.000GB
test 0.000GB
> use mongo_practice
switched to db mongo_practice
> db.movies.insertMany([{title:"Fight Club", writer:"Chuck palahniuk", year:1999 , actors:["Brad Pitt", "Edward Norton"]},
{title:"Pulp Fiction", writer:"quentin Taratino", year:1994, actors:["John Travolta", "Uma Thurman"]},
{title:"Inglorious Basterds", writer:"Quentin Taratino", year:2009, actors:["Brad Pitt", "Diane Kruger", "Eli Roth"]},
{title:"The Hobbit: An Unexpected Journey", writer:"J.R.R Tolkein", year:2012, franchise:"The Hobbit"},
{title:"The Hobbit: The Desolution of Smaug",writer:"J.R.R. Tolkein", year:2013, franchise:"The Hobbit"},
{title:"The Hobbit: The Battle of the Five Armies", writer: "J.R.R Tolkein", year:2012, franchise:"The Hobbit", Synopsis:"Bilbo and company are forced to engage in a war against the array of combatants and keep the Lonely Mountain from falling into the hands of a rising Darkness."},
{title:"Pee Wee Herman's Big Adventure"},
{title:"Avatar"}])
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("62ab6106ea8131187f213318"),
ObjectId("62ab6106ea8131187f213319"),
ObjectId("62ab6106ea8131187f21331a"),
ObjectId("62ab6106ea8131187f21331b"),
ObjectId("62ab6106ea8131187f21331c"),
ObjectId("62ab6106ea8131187f21331d"),
ObjectId("62ab6106ea8131187f21331e"),
ObjectId("62ab6106ea8131187f21331f")
]
}
> db.movies.find().pretty()
{
"_id" : ObjectId("62a70671bbabcd02f61d2ba8"),
"title" : "Fight Club",
"writer" : "Chuck palahniuk",
"year" : 1999,
"actors" : "[Brad Pitt, Edward Norton]"
}
{
"_id" : ObjectId("62a70671bbabcd02f61d2ba9"),
"title" : "Pulp Fiction",
"writer" : "Quentin Taratino",
"year" : 1994,
"actors" : "[John Travolta, Uma Thurman]"
}
{
"_id" : ObjectId("62a70671bbabcd02f61d2baa"),
"title" : "Inglorious Basterds",
"writer" : "Quentin Taratino",
"year" : 2009,
"actors" : "[Brad Pitt, Diane Kruger, Eli Roth]"
}
{
"_id" : ObjectId("62a70671bbabcd02f61d2bab"),
"title" : "The Hobbit: An Unexpected Journey",
"writer" : "J.R.R Tolkein",
"year" : 2012,
"franchise" : "The Hobbit"
}
{
"_id" : ObjectId("62a70671bbabcd02f61d2bac"),
"title" : "The Hobbit: The Desolution of Smaug",
"writer" : "J.R.R. Tolkein",
"year" : 2013,
"franchise" : "The Hobbit"
}
{
"_id" : ObjectId("62a70671bbabcd02f61d2bad"),
"title" : "The Hobbit: The Battle of the Five Armies",
"writer" : "J.R.R Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"Synopsis" : "Bilbo and company are forced to engage in a war against the array of combatants and keep the Lonely Mountain from falling into the hands of a rising Darkness."
}
{
"_id" : ObjectId("62a70671bbabcd02f61d2bae"),
"title" : "Pee Wee Herman's Big Adventure"
}
{ "_id" : ObjectId("62a70671bbabcd02f61d2baf"), "title" : "Avatar" }
> db.movies.find({actors:"Brad Pitt"}).pretty()
{
"_id" : ObjectId("62ab5ed44ffe7a06b5f0729d"),
"title" : "Fight Club",
"writer" : "Chuck palahniuk",
"year" : 1999,
"actors" : [
"Brad Pitt",
"Edward Norton"
]
}
{
"_id" : ObjectId("62ab5ed44ffe7a06b5f0729f"),
"title" : "Inglorious Basterds",
"writer" : "Quentin Taratino",
"year" : 2009,
"actors" : [
"Brad Pitt",
"Diane Kruger",
"Eli Roth"
]
}
> db.movies.find({writer:"Quentin Taratino"}).pretty()
{
"_id" : ObjectId("62a70671bbabcd02f61d2ba9"),
"title" : "Pulp Fiction",
"writer" : "Quentin Taratino",
"year" : 1994,
"actors" : "[John Travolta, Uma Thurman]"
}
{
"_id" : ObjectId("62a70671bbabcd02f61d2baa"),
"title" : "Inglorious Basterds",
"writer" : "Quentin Taratino",
"year" : 2009,
"actors" : "[Brad Pitt, Diane Kruger, Eli Roth]"
}
> db.movies.find({franchise:"The Hobbit"}).pretty()
{
"_id" : ObjectId("62a70671bbabcd02f61d2bab"),
"title" : "The Hobbit: An Unexpected Journey",
"writer" : "J.R.R Tolkein",
"year" : 2012,
"franchise" : "The Hobbit"
}
{
"_id" : ObjectId("62a70671bbabcd02f61d2bac"),
"title" : "The Hobbit: The Desolution of Smaug",
"writer" : "J.R.R. Tolkein",
"year" : 2013,
"franchise" : "The Hobbit"
}
{
"_id" : ObjectId("62a70671bbabcd02f61d2bad"),
"title" : "The Hobbit: The Battle of the Five Armies",
"writer" : "J.R.R Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"Synopsis" : "Bilbo and company are forced to engage in a war against the array of combatants and keep the Lonely Mountain from falling into the hands of a rising Darkness."
}
> db.movies.find({year:{$lt:2000}}).pretty()
{
"_id" : ObjectId("62a70671bbabcd02f61d2ba8"),
"title" : "Fight Club",
"writer" : "Chuck palahniuk",
"year" : 1999,
"actors" : "[Brad Pitt, Edward Norton]"
}
{
"_id" : ObjectId("62a70671bbabcd02f61d2ba9"),
"title" : "Pulp Fiction",
"writer" : "Quentin Taratino",
"year" : 1994,
"actors" : "[John Travolta, Uma Thurman]"
}
> db.movies.find({year:{$gt:2010}}).pretty()
{
"_id" : ObjectId("62a70671bbabcd02f61d2bab"),
"title" : "The Hobbit: An Unexpected Journey",
"writer" : "J.R.R Tolkein",
"year" : 2012,
"franchise" : "The Hobbit"
}
{
"_id" : ObjectId("62a70671bbabcd02f61d2bac"),
"title" : "The Hobbit: The Desolution of Smaug",
"writer" : "J.R.R. Tolkein",
"year" : 2013,
"franchise" : "The Hobbit"
}
{
"_id" : ObjectId("62a70671bbabcd02f61d2bad"),
"title" : "The Hobbit: The Battle of the Five Armies",
"writer" : "J.R.R Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"Synopsis" : "Bilbo and company are forced to engage in a war against the array of combatants and keep the Lonely Mountain from falling into the hands of a rising Darkness."
} |
use mongo_practiceuse mongo_practice
switched to db mongo_practice
db.movies.insertMany([{title: "Fight Club", writer: "Chuck Palahniuk", year: 1999, actors: ["Brad Pitt", "EEdward Norton"]},
... {title: "Pulp Fiction", writer: "Quentin Tarantino", year: 1994, actors: ["John Travolta", "Uma Thurman"]},
... {title: "Inglorius Basterds", writer: "Quentin Tarantino", year: 2009, actors: ["Brad Pitt", "Diane Kruger", "Eli Roth"]},
... {title: "The Hobbit: An Unexpected Journey", writer: "J.R.R. Tolkein", year: 2012, franchise: "The Hobbit"},
... {title: "The Hobbit: The Desolation of smug", writer: "J.R.R. Tolkein", year: 2013, franchise: "The Hobbit"},
... {title: "The Hobbit: The Battle of the Five Armies", writer: "J.R.R. Tolkein", year: 2012, franchise: "The Hobbit",
... synopsis: "Bilbo and Company are forced to engage in a war against an array of combatants and keep the the lonely mountains from falling into the hands of a rising darkness."},
... {title: "Pee Wee Herman's Big Adventure"}, {title: "Avatar"}]);
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("62ad77b1066c739c48ed331b"),
ObjectId("62ad77b1066c739c48ed331c"),
ObjectId("62ad77b1066c739c48ed331d"),
ObjectId("62ad77b1066c739c48ed331e"),
ObjectId("62ad77b1066c739c48ed331f"),
ObjectId("62ad77b1066c739c48ed3320"),
ObjectId("62ad77b1066c739c48ed3321"),
ObjectId("62ad77b1066c739c48ed3322")
]
}
db.movies.find({}).pretty();
{
"_id" : ObjectId("62ad77b1066c739c48ed331b"),
"title" : "Fight Club",
"writer" : "Chuck Palahniuk",
"year" : 1999,
"actors" : [
"Brad Pitt",
"EEdward Norton"
]
}
{
"_id" : ObjectId("62ad77b1066c739c48ed331c"),
"title" : "Pulp Fiction",
"writer" : "Quentin Tarantino",
"year" : 1994,
"actors" : [
"John Travolta",
"Uma Thurman"
]
}
{
"_id" : ObjectId("62ad77b1066c739c48ed331d"),
"title" : "Inglorius Basterds",
"writer" : "Quentin Tarantino",
"year" : 2009,
"actors" : [
"Brad Pitt",
"Diane Kruger",
"Eli Roth"
]
}
{
"_id" : ObjectId("62ad77b1066c739c48ed331e"),
"title" : "The Hobbit: An Unexpected Journey",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit"
}
{
"_id" : ObjectId("62ad77b1066c739c48ed331f"),
"title" : "The Hobbit: The Desolation of smug",
"writer" : "J.R.R. Tolkein",
"year" : 2013,
"franchise" : "The Hobbit"
}
{
"_id" : ObjectId("62ad77b1066c739c48ed3320"),
"title" : "The Hobbit: The Battle of the Five Armies",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : "Bilbo and Company are forced to engage in a war against an array of combatants and keep the the lonely mountains from falling into the hands of a rising darkness."
}
{
"_id" : ObjectId("62ad77b1066c739c48ed3321"),
"title" : "Pee Wee Herman's Big Adventure"
}
{ "_id" : ObjectId("62ad77b1066c739c48ed3322"), "title" : "Avatar" }
db.movies.find({writer:"Quentin Tarantino"}).pretty();
{
"_id" : ObjectId("62ad77b1066c739c48ed331c"),
"title" : "Pulp Fiction",
"writer" : "Quentin Tarantino",
"year" : 1994,
"actors" : [
"John Travolta",
"Uma Thurman"
]
}
{
"_id" : ObjectId("62ad77b1066c739c48ed331d"),
"title" : "Inglorius Basterds",
"writer" : "Quentin Tarantino",
"year" : 2009,
"actors" : [
"Brad Pitt",
"Diane Kruger",
"Eli Roth"
]
}
db.movies.find({actors:"Brad Pitt"}).pretty();
{
"_id" : ObjectId("62ad77b1066c739c48ed331b"),
"title" : "Fight Club",
"writer" : "Chuck Palahniuk",
"year" : 1999,
"actors" : [
"Brad Pitt",
"EEdward Norton"
]
}
{
"_id" : ObjectId("62ad77b1066c739c48ed331d"),
"title" : "Inglorius Basterds",
"writer" : "Quentin Tarantino",
"year" : 2009,
"actors" : [
"Brad Pitt",
"Diane Kruger",
"Eli Roth"
]
}
db.movies.find({franchise : "The Hobbit"}).pretty();
{
"_id" : ObjectId("62ad77b1066c739c48ed331e"),
"title" : "The Hobbit: An Unexpected Journey",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit"
}
{
"_id" : ObjectId("62ad77b1066c739c48ed331f"),
"title" : "The Hobbit: The Desolation of smug",
"writer" : "J.R.R. Tolkein",
"year" : 2013,
"franchise" : "The Hobbit"
}
{
"_id" : ObjectId("62ad77b1066c739c48ed3320"),
"title" : "The Hobbit: The Battle of the Five Armies",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : "Bilbo and Company are forced to engage in a war against an array of combatants and keep the the lonely mountains from falling into the hands of a rising darkness."
}
db.movies.find({$and: [{year: {$gt: 1900}}, {year: {$lt: 2000}}]},{title:1,_id:0}).pretty();
{ "title" : "Fight Club" }
{ "title" : "Pulp Fiction" }
db.movies.find({$or:[{year:{$lt:2000}},{year:{$gt:2010}}]},{title:1,_id:0}).pretty();
{ "title" : "Fight Club" }
{ "title" : "Pulp Fiction" }
{ "title" : "The Hobbit: An Unexpected Journey" }
{ "title" : "The Hobbit: The Desolation of smug" }
{ "title" : "The Hobbit: The Battle of the Five Armies" }
//update
db.movies.update({ title: 'The Hobbit: An Unexpected Journey' },
{ $set: { synopsis: "A reluctant hobbit, Bilbo Baggins, sets out to the Lonely Mountain with a spirited group of dwarves to reclaim their mountain home - and the gold within it - from the dragon Smaug." } });
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
{
"_id" : ObjectId("62ad77b1066c739c48ed331e"),
"title" : "The Hobbit: An Unexpected Journey",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : "A reluctant hobbit, Bilbo Baggins, sets out to the Lonely Mountain with a spirited group of dwarves to reclaim their mountain home - and the gold within it - from the dragon Smaug."
}
db.movies.update({ title : "The Hobbit: The Desolation of smug"}, { $set: { synopsis: "The dwarves, along with Bilbo Baggins and Gandalf the Grey, continue their quest to reclaim Erebor, their homeland, from Smaug. Bilbo Baggins is in possession of a mysterious and magical ring." } });
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
{
"_id" : ObjectId("62ad77b1066c739c48ed331f"),
"title" : "The Hobbit: The Desolation of smug",
"writer" : "J.R.R. Tolkein",
"year" : 2013,
"franchise" : "The Hobbit",
"synopsis" : "The dwarves, along with Bilbo Baggins and Gandalf the Grey, continue their quest to reclaim Erebor, their homeland, from Smaug. Bilbo Baggins is in possession of a mysterious and magical ring."
}
db.movies.update({ title: 'Pulp Fiction' }, { $push: { actors: 'Samuel L. Jackson' } });
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
{
"_id" : ObjectId("62ad77b1066c739c48ed331c"),
"title" : "Pulp Fiction",
"writer" : "Quentin Tarantino",
"year" : 1994,
"actors" : [
"John Travolta",
"Uma Thurman",
"Samuel L. Jackson"
]
}
//Text Search
db.movies.find({"synopsis":{"$regex":"Bilbo"}},{title:1,synopsis:1,_id:0}).pretty();
{
"title" : "The Hobbit: An Unexpected Journey",
"synopsis" : "A reluctant hobbit, Bilbo Baggins, sets out to the Lonely Mountain with a spirited group of dwarves to reclaim their mountain home - and the gold within it - from the dragon Smaug."
}
{
"title" : "The Hobbit: The Desolation of smug",
"synopsis" : "The dwarves, along with Bilbo Baggins and Gandalf the Grey, continue their quest to reclaim Erebor, their homeland, from Smaug. Bilbo Baggins is in possession of a mysterious and magical ring."
}
{
"title" : "The Hobbit: The Battle of the Five Armies",
"synopsis" : "Bilbo and Company are forced to engage in a war against an array of combatants and keep the the lonely mountains from falling into the hands of a rising darkness."
}
db.movies.find({"synopsis":{"$regex":"Gandalf"}},{title:1,synopsis:1,_id:0}).pretty();
{
"title" : "The Hobbit: The Desolation of smug",
"synopsis" : "The dwarves, along with Bilbo Baggins and Gandalf the Grey, continue their quest to reclaim Erebor, their homeland, from Smaug. Bilbo Baggins is in possession of a mysterious and magical ring."
}
db.movies.find({"synopsis":{"$regex":"Bilbo",$not:{"$regex":"Gandalf"}}},{title:1,synopsis:1,_id:0}).pretty();
{
"title" : "The Hobbit: An Unexpected Journey",
"synopsis" : "A reluctant hobbit, Bilbo Baggins, sets out to the Lonely Mountain with a spirited group of dwarves to reclaim their mountain home - and the gold within it - from the dragon Smaug."
}
{
"title" : "The Hobbit: The Battle of the Five Armies",
"synopsis" : "Bilbo and Company are forced to engage in a war against an array of combatants and keep the the lonely mountains from falling into the hands of a rising darkness."
}
db.movies.find({"$or":[{"synopsis":{"$regex":"dwarves"}},{"synopsis":{"$regex":"hobbit"}}]},{title:1,synopsis:1,_id:0}).pretty();
{
"title" : "The Hobbit: An Unexpected Journey",
"synopsis" : "A reluctant hobbit, Bilbo Baggins, sets out to the Lonely Mountain with a spirited group of dwarves to reclaim their mountain home - and the gold within it - from the dragon Smaug."
}
{
"title" : "The Hobbit: The Desolation of smug",
"synopsis" : "The dwarves, along with Bilbo Baggins and Gandalf the Grey, continue their quest to reclaim Erebor, their homeland, from Smaug. Bilbo Baggins is in possession of a mysterious and magical ring."
}
db.movies.find({"$and":[{"synopsis":{"$regex":"gold"}},{"synopsis":{"$regex":"dragon"}}]},{title:1,synopsis:1,_id:0}).pretty();
{
"title" : "The Hobbit: An Unexpected Journey",
"synopsis" : "A reluctant hobbit, Bilbo Baggins, sets out to the Lonely Mountain with a spirited group of dwarves to reclaim their mountain home - and the gold within it - from the dragon Smaug."
}
//Delete documents
db.movies.deleteMany({ title: "Pee Wee Herman's Big Adventure" });
{ "acknowledged" : true, "deletedCount" : 1 }
db.movies.deleteMany({ title: "Avatar" });
{ "acknowledged" : true, "deletedCount" : 1 }
db.movies.find({},{title:1,_id:0}).pretty();
{ "title" : "Fight Club" }
{ "title" : "Pulp Fiction" }
{ "title" : "Inglorius Basterds" }
{ "title" : "The Hobbit: An Unexpected Journey" }
{ "title" : "The Hobbit: The Desolation of smug" }
{ "title" : "The Hobbit: The Battle of the Five Armies" }
//Relationships
//users collection
db.users.insertMany([{"username":"GoodGuyGreg","first_name":"Good Guy", "last_name" : "Greg"},{ "username" : "ScumbagSteve", "full_name" : {"first" : "Scumbag" , "last" : "Steve"}}])
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("62b0abe3967866aa1e957546"),
ObjectId("62b0abe3967866aa1e957547")
]
}
// posts collections
db.posts.insertMany([{"username":"GoodGuyGreg","title" :" Passes out at party", "body" :" Wakes up early and cleans house"},{"username":"GoodGuyGreg","title" :"Steals your identity", "body" :"Raises your credit score"},{"username":"GoodGuyGreg","title" :" Reports a bug in your code", "body" :" Sends you a Pull Request"},{"username":"ScumbagSteve","title" :" Borrows something", "body" :"Sells it"},{"username":"ScumbagSteve","title" :"Borrows everything", "body" :"The end"},{"username":"ScumbagSteve","title" :"Forks your repo on github", "body" :"Sets to private"}])
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("62b0acab967866aa1e957548"),
ObjectId("62b0acab967866aa1e957549"),
ObjectId("62b0acab967866aa1e95754a"),
ObjectId("62b0acab967866aa1e95754b"),
ObjectId("62b0acab967866aa1e95754c"),
ObjectId("62b0acab967866aa1e95754d")
]
}
//comments collection
db.comments.insertMany(
[{
username : "GoodGuyGreg",
comment : "Hope you got a good deal!",
post : ObjectId("62b0acab967866aa1e95754b")
},
{
username : "GoodGuyGreg",
comment : "What's mine is yours! ",
post : ObjectId("62b0acab967866aa1e95754c")
},
{
username : "GoodGuyGreg",
comment : "Don't violate the licensing agreement! ",
post : ObjectId("62b0acab967866aa1e95754d")
},
{
username : "ScumbagSteve",
comment : "It still isn't clean",
post : ObjectId("62b0acab967866aa1e957548"),
},
{
username : "ScumbagSteve",
comment : "Denied your PR cause I found a hack",
post : ObjectId("62b0acab967866aa1e95754a")
}]);;
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("62b31cdd277a41f1d848be8e"),
ObjectId("62b31cdd277a41f1d848be8f"),
ObjectId("62b31cdd277a41f1d848be90"),
ObjectId("62b31cdd277a41f1d848be91"),
ObjectId("62b31cdd277a41f1d848be92")
]
}
// find users
db.users.find().pretty();
{
"_id" : ObjectId("62b0abe3967866aa1e957546"),
"username" : "GoodGuyGreg",
"first_name" : "Good Guy",
"last_name" : "Greg"
}
{
"_id" : ObjectId("62b0abe3967866aa1e957547"),
"username" : "ScumbagSteve",
"full_name" : {
"first" : "Scumbag",
"last" : "Steve"
}
}
// find posts
> db.posts.find().pretty();
{
"_id" : ObjectId("62b0acab967866aa1e957548"),
"username" : "GoodGuyGreg",
"title" : " Passes out at party",
"body" : " Wakes up early and cleans house"
}
{
"_id" : ObjectId("62b0acab967866aa1e957549"),
"username" : "GoodGuyGreg",
"title" : "Steals your identity",
"body" : "Raises your credit score"
}
{
"_id" : ObjectId("62b0acab967866aa1e95754a"),
"username" : "GoodGuyGreg",
"title" : " Reports a bug in your code",
"body" : " Sends you a Pull Request"
}
{
"_id" : ObjectId("62b0acab967866aa1e95754b"),
"username" : "ScumbagSteve",
"title" : " Borrows something",
"body" : "Sells it"
}
{
"_id" : ObjectId("62b0acab967866aa1e95754c"),
"username" : "ScumbagSteve",
"title" : "Borrows everything",
"body" : "The end"
}
{
"_id" : ObjectId("62b0acab967866aa1e95754d"),
"username" : "ScumbagSteve",
"title" : "Forks your repo on github",
"body" : "Sets to private"
}
//
db.posts.find({ username: 'GoodGuyGreg' }).pretty();
{
"_id" : ObjectId("62b0acab967866aa1e957548"),
"username" : "GoodGuyGreg",
"title" : " Passes out at party",
"body" : " Wakes up early and cleans house"
}
{
"_id" : ObjectId("62b0acab967866aa1e957549"),
"username" : "GoodGuyGreg",
"title" : "Steals your identity",
"body" : "Raises your credit score"
}
{
"_id" : ObjectId("62b0acab967866aa1e95754a"),
"username" : "GoodGuyGreg",
"title" : " Reports a bug in your code",
"body" : " Sends you a Pull Request"
}
db.posts.find({ username: 'ScumbagSteve' }).pretty();
{
"_id" : ObjectId("62b0acab967866aa1e95754b"),
"username" : "ScumbagSteve",
"title" : " Borrows something",
"body" : "Sells it"
}
{
"_id" : ObjectId("62b0acab967866aa1e95754c"),
"username" : "ScumbagSteve",
"title" : "Borrows everything",
"body" : "The end"
}
{
"_id" : ObjectId("62b0acab967866aa1e95754d"),
"username" : "ScumbagSteve",
"title" : "Forks your repo on github",
"body" : "Sets to private"
}
db.comments.find().pretty();
{
"_id" : ObjectId("62b0afe5967866aa1e95754e"),
"username" : "GoodGuyGreg",
"comment" : "Hope you got a good deal!",
"post" : ObjectId("5f44d3a148197d7749864def")
}
{
"_id" : ObjectId("62b0afe5967866aa1e95754f"),
"username" : "GoodGuyGreg",
"comment" : "Don't violate the licensing agreement!",
"post" : ObjectId("5f44d3a148197d7749864df0")
}
{
"_id" : ObjectId("62b0afe5967866aa1e957550"),
"username" : "GoodGuyGreg",
"comment" : "Don't violate the licensing agreement!",
"post" : ObjectId("5f44d3a148197d7749864df1")
}
{
"_id" : ObjectId("62b0afe5967866aa1e957551"),
"username" : "ScumbagSteve",
"comment" : "It still isn't clean",
"post" : ObjectId("5f44d3a148197d7749864dec")
}
{
"_id" : ObjectId("62b0afe5967866aa1e957552"),
"username" : "ScumbagSteve",
"comment" : "Denied your PR cause I found a hack",
"post" : ObjectId("5f44d3a148197d7749864dee")
}
db.comments.find({ username: 'GoodGuyGreg' }).pretty();
{
"_id" : ObjectId("62b0afe5967866aa1e95754e"),
"username" : "GoodGuyGreg",
"comment" : "Hope you got a good deal!",
"post" : ObjectId("5f44d3a148197d7749864def")
}
{
"_id" : ObjectId("62b0afe5967866aa1e95754f"),
"username" : "GoodGuyGreg",
"comment" : "Don't violate the licensing agreement!",
"post" : ObjectId("5f44d3a148197d7749864df0")
}
{
"_id" : ObjectId("62b0afe5967866aa1e957550"),
"username" : "GoodGuyGreg",
"comment" : "Don't violate the licensing agreement!",
"post" : ObjectId("5f44d3a148197d7749864df1")
}
db.comments.find({ username: 'ScumbagSteve' }).pretty();
{
"_id" : ObjectId("62b0afe5967866aa1e957551"),
"username" : "ScumbagSteve",
"comment" : "It still isn't clean",
"post" : ObjectId("5f44d3a148197d7749864dec")
}
{
"_id" : ObjectId("62b0afe5967866aa1e957552"),
"username" : "ScumbagSteve",
"comment" : "Denied your PR cause I found a hack",
"post" : ObjectId("5f44d3a148197d7749864dee")
}
//8
db.posts.aggregate([
... {
... $match: { "title" : " Reports a bug in your code" }
... },
... {
... $lookup: {
... from: 'comments',
... localField: '_id',
... foreignField: 'post',
... as: 'comments'
... }
... }
... ]).pretty();
{
"_id" : ObjectId("62b0acab967866aa1e95754a"),
"username" : "GoodGuyGreg",
"title" : " Reports a bug in your code",
"body" : " Sends you a Pull Request",
"comments" : [
{
"_id" : ObjectId("62b31cdd277a41f1d848be92"),
"username" : "ScumbagSteve",
"comment" : "Denied your PR cause I found a hack",
"post" : ObjectId("62b0acab967866aa1e95754a")
}
]
}
|
> use mongo_practice
switched to db mongo_practice
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
mongo_practice 0.000GB
testDb 0.000GB
> show collections
movies
>db.movies.insertMany([{ title: "Fight Club", writer: "Chuck Palahniuk", year: 1999, actors: ["Brad Pitt", "EEdward Norton"]},
... {title: "Pulp Fiction", writer: "Quentin Tarantino", year: 1994, actors: ["John Travolta", "Uma Thurman"]},
... {title: "Inglorius Basterds", writer: "Quentin Tarantino", year: 2009, actors: ["Brad Pitt", "Diane Kruger", "Eli Roth"]},
... {title: "The Hobbit: An Unexpected Journey", writer: "J.R.R. Tolkein", year: 2012, franchise: "The Hobbit"},
... {title: "The Hobbit: The Desolation of smug", writer: "J.R.R. Tolkein", year: 2013, franchise: "The Hobbit"},
... {title: "The Hobbit: The Battle of the Five Armies", writer: "J.R.R. Tolkein", year: 2012, franchise: "The Hobbit", synopsis: "Bilbo and Company are forced to engage in a war against an array of combatants and keep the the lonely mountains from falling into the hands of a rising darkness."},
... {title: "Pee Wee Herman's Big Adventure"}, {title: "Avatar"}]);
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("62adbe75fcfadd26599efc94"),
ObjectId("62adbe75fcfadd26599efc95"),
ObjectId("62adbe75fcfadd26599efc96"),
ObjectId("62adbe75fcfadd26599efc97"),
ObjectId("62adbe75fcfadd26599efc98"),
ObjectId("62adbe75fcfadd26599efc99"),
ObjectId("62adbe75fcfadd26599efc9a"),
ObjectId("62adbe75fcfadd26599efc9b")
]
}
> db.movies.find().pretty();
{
"_id" : ObjectId("62adbe75fcfadd26599efc94"),
"title" : "Fight Club",
"writer" : "Chuck Palahniuk",
"year" : 1999,
"actors" : [
"Brad Pitt",
"EEdward Norton"
]
}
{
"_id" : ObjectId("62adbe75fcfadd26599efc95"),
"title" : "Pulp Fiction",
"writer" : "Quentin Tarantino",
"year" : 1994,
"actors" : [
"John Travolta",
"Uma Thurman"
]
}
{
"_id" : ObjectId("62adbe75fcfadd26599efc96"),
"title" : "Inglorius Basterds",
"writer" : "Quentin Tarantino",
"year" : 2009,
"actors" : [
"Brad Pitt",
"Diane Kruger",
"Eli Roth"
]
}
{
"_id" : ObjectId("62adbe75fcfadd26599efc97"),
"title" : "The Hobbit: An Unexpected Journey",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit"
}
{
"_id" : ObjectId("62adbe75fcfadd26599efc98"),
"title" : "The Hobbit: The Desolation of smug",
"writer" : "J.R.R. Tolkein",
"year" : 2013,
"franchise" : "The Hobbit"
}
{
"_id" : ObjectId("62adbe75fcfadd26599efc99"),
"title" : "The Hobbit: The Battle of the Five Armies",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : "Bilbo and Company are forced to engage in a war against an array of combatants and keep the the lonely mountains from falling into the hands of a rising darkness."
}
{
"_id" : ObjectId("62adbe75fcfadd26599efc9a"),
"title" : "Pee Wee Herman's Big Adventure"
}
{ "_id" : ObjectId("62adbe75fcfadd26599efc9b"), "title" : "Avatar" }
|
|
Code> use mongo_practice
switched to db mongo_practice
> db.movies.insertMany([{title: "Fight Club", writer: "Chuck Palahniuk", year: 1999, actors: ["Brad Pitt", "EEdward Norton"]},
{title: "Pulp Fiction", writer: "Quentin Tarantino", year: 1994, actors: ["John Travolta", "Uma Thurman"]},
{title: "Inglorius Basterds", writer: "Quentin Tarantino", year: 2009, actors: ["Brad Pitt", "Diane Kruger", "Eli Roth"]},
{title: "The Hobbit: An Unexpected Journey", writer: "J.R.R. Tolkein", year: 2012, franchise: "The Hobbit"},
{title: "The Hobbit: The Desolation of smug", writer: "J.R.R. Tolkein", year: 2013, franchise: "The Hobbit"},
{title: "The Hobbit: The Battle of the Five Armies", writer: "J.R.R. Tolkein", year: 2012, franchise: "The Hobbit",
synopsis: "Bilbo and Company are forced to engage in a war against an array of combatants and keep the the lonely mountains from falling into the hands of a rising darkness."},
{title: "Pee Wee Herman's Big Adventure"}, {title: "Avatar"}]);
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("62a78b4b7960f45ec00b89bb"),
ObjectId("62a78b4b7960f45ec00b89bc"),
ObjectId("62a78b4b7960f45ec00b89bd"),
ObjectId("62a78b4b7960f45ec00b89be"),
ObjectId("62a78b4b7960f45ec00b89bf"),
ObjectId("62a78b4b7960f45ec00b89c0"),
ObjectId("62a78b4b7960f45ec00b89c1"),
ObjectId("62a78b4b7960f45ec00b89c2")
]
}
> db.movies.find().pretty()
{
"_id" : ObjectId("62aef58297b534010f70dbba"),
"title" : "Fight Club",
"writer" : "Chuck Palahniuk",
"year" : 1999,
"actors" : [
"Brad Pitt",
"Edward Norton"
]
}
{
"_id" : ObjectId("62aef58297b534010f70dbbb"),
"title" : "Pulp Fiction",
"writer" : "Quentin Tarantino",
"year" : 1994,
"actors" : [
"John Travolta",
"Uma Thurman"
]
}
{
"_id" : ObjectId("62aef58297b534010f70dbbc"),
"title" : "Inglorious Basterds",
"writer" : "Quentin Tarantino",
"year" : 2009,
"actors" : [
"Brad Pitt",
"Diane Kruger",
"Eli Roth"
]
}
{
"_id" : ObjectId("62aef58297b534010f70dbbd"),
"title" : "The Hobbit:An Unexpected Journey",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit"
}
{
"_id" : ObjectId("62aef58297b534010f70dbbe"),
"title" : "The Hobbit: The Desolation of Smaug",
"writer" : "J.R.R. Tolkein",
"year" : 2013,
"franchise" : "The Hobbit"
}
{
"_id" : ObjectId("62aef58297b534010f70dbbf"),
"title" : "The Hobbit: The Battle of the Five Armies",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : "Bilbo and Company are forced to engage in a war against an array of combatants and keep the Lonely Mountain from falling into the hands of a rising darkness."
}
{
"_id" : ObjectId("62aef58297b534010f70dbc0"),
"title" : "Pee Wee Herman's Big Adventure"
}
{ "_id" : ObjectId("62aef58297b534010f70dbc1"), "title" : "Avatar" }
> db.movies.find({writer:"Quentin Tarantino"}).pretty()
{
"_id" : ObjectId("62aef58297b534010f70dbbb"),
"title" : "Pulp Fiction",
"writer" : "Quentin Tarantino",
"year" : 1994,
"actors" : [
"John Travolta",
"Uma Thurman"
]
}
{
"_id" : ObjectId("62aef58297b534010f70dbbc"),
"title" : "Inglorious Basterds",
"writer" : "Quentin Tarantino",
"year" : 2009,
"actors" : [
"Brad Pitt",
"Diane Kruger",
"Eli Roth"
]
}
> db.movies.find( {actors : { $in : ["Brad Pitt"]}}).pretty();
{
"_id" : ObjectId("62aef58297b534010f70dbba"),
"title" : "Fight Club",
"writer" : "Chuck Palahniuk",
"year" : 1999,
"actors" : [
"Brad Pitt",
"Edward Norton"
]
}
{
"_id" : ObjectId("62aef58297b534010f70dbbc"),
"title" : "Inglorious Basterds",
"writer" : "Quentin Tarantino",
"year" : 2009,
"actors" : [
"Brad Pitt",
"Diane Kruger",
"Eli Roth"
]
}
> db.movies.find({franchise:"The Hobbit"}).pretty()
{
"_id" : ObjectId("62aef58297b534010f70dbbd"),
"title" : "The Hobbit:An Unexpected Journey",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit"
}
{
"_id" : ObjectId("62aef58297b534010f70dbbe"),
"title" : "The Hobbit: The Desolation of Smaug",
"writer" : "J.R.R. Tolkein",
"year" : 2013,
"franchise" : "The Hobbit"
}
{
"_id" : ObjectId("62aef58297b534010f70dbbf"),
"title" : "The Hobbit: The Battle of the Five Armies",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : "Bilbo and Company are forced to engage in a war against an array of combatants and keep the Lonely Mountain from falling into the hands of a rising darkness."
}
> db.movies.find({$and:[{year:{$gte:1990}},{year:{$lte:1999}}]}).pretty()
{
"_id" : ObjectId("62aef58297b534010f70dbba"),
"title" : "Fight Club",
"writer" : "Chuck Palahniuk",
"year" : 1999,
"actors" : [
"Brad Pitt",
"Edward Norton"
]
}
{
"_id" : ObjectId("62aef58297b534010f70dbbb"),
"title" : "Pulp Fiction",
"writer" : "Quentin Tarantino",
"year" : 1994,
"actors" : [
"John Travolta",
"Uma Thurman"
]
}
> db.movies.find({$or:[{year:{$gt:2010}},{year:{$lt:2000}}]}).pretty()
{
"_id" : ObjectId("62aef58297b534010f70dbba"),
"title" : "Fight Club",
"writer" : "Chuck Palahniuk",
"year" : 1999,
"actors" : [
"Brad Pitt",
"Edward Norton"
]
}
{
"_id" : ObjectId("62aef58297b534010f70dbbb"),
"title" : "Pulp Fiction",
"writer" : "Quentin Tarantino",
"year" : 1994,
"actors" : [
"John Travolta",
"Uma Thurman"
]
}
{
"_id" : ObjectId("62aef58297b534010f70dbbd"),
"title" : "The Hobbit:An Unexpected Journey",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit"
}
{
"_id" : ObjectId("62aef58297b534010f70dbbe"),
"title" : "The Hobbit: The Desolation of Smaug",
"writer" : "J.R.R. Tolkein",
"year" : 2013,
"franchise" : "The Hobbit"
}
{
"_id" : ObjectId("62aef58297b534010f70dbbf"),
"title" : "The Hobbit: The Battle of the Five Armies",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : "Bilbo and Company are forced to engage in a war against an array of combatants and keep the Lonely Mountain from falling into the hands of a rising darkness."
}
> db.movies.updateOne({title:"The Hobbit:An Unexpected Journey"},{$set:{synopsis:"A reluctant hobbit, Bilbo Baggins, sets out to the Lonely Mountain with a spirited group of dwarves to reclaim their mountain home - and the gold within it - from the dragon Smaug."}})
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
> db.movies.updateOne({title:"The Hobbit: The Desolation of Smaug"},{$set:{synopsis:"The dwarves, along with Bilbo Baggins and Gandalf the Grey, continue their quest to reclaim Erebor, their homeland, from Smaug. Bilbo Baggins is in possession of a mysterious and magical ring."}})
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
> db.movies.updateOne({title:"Pulp Fiction"},{$push:{actor:{"Samuel L. Jackson"}}})
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
> db.movies.find({"title":"Pulp Fiction"},{_id:0,title:1,actors:1})
{ "title" : "Pulp Fiction", "actors" : [ "John Travolta", "Uma Thurman", "Samuel L. Jackson" ] }
> db.movies.find({"synopsis":{"$regex":"Bilbo"}}).pretty()
{
"_id" : ObjectId("62a81d49a0464c977fa8f2e5"),
"title" : "The Hobbit: An Unexpected Journey",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : "A reluctant hobbit, Bilbo Baggins, sets out to the Lonely Mountain with a spirited group of dwarves to reclaim their mountain home - and the gold within it - from the dragon Smaug."
}
{
"_id" : ObjectId("62a81d49a0464c977fa8f2e6"),
"title" : "The Hobbit: The Desolation of Smaug",
"writer" : "J.R.R. Tolkein",
"year" : 2013,
"franchise" : "The Hobbit",
"synopsis" : "The dwarves, along with Bilbo Baggins and Gandalf the Grey, continue their quest to reclaim Erebor, their homeland, from Smaug. Bilbo Baggins is in possession of a mysterious and magical ring."
}
{
"_id" : ObjectId("62a81d49a0464c977fa8f2e7"),
"title" : "The Hobbit: The Battle of the Five Armies",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : "Bilbo and Company are forced to engage in a war against an array of combatants and keep the the lonely mountains from falling into the hands of a rising darkness."
}
> db.movies.find({"synopsis":{"$regex":"Gandalf"}}).pretty()
{
"_id" : ObjectId("62a81d49a0464c977fa8f2e6"),
"title" : "The Hobbit: The Desolation of Smaug",
"writer" : "J.R.R. Tolkein",
"year" : 2013,
"franchise" : "The Hobbit",
"synopsis" : "The dwarves, along with Bilbo Baggins and Gandalf the Grey, continue their quest to reclaim Erebor, their homeland, from Smaug. Bilbo Baggins is in possession of a mysterious and magical ring."
}
> db.movies.find({"synopsis":{"$regex":"Bilbo",$not:{"$regex":"Gandalf"}}}).pretty()
{
"_id" : ObjectId("62a81d49a0464c977fa8f2e5"),
"title" : "The Hobbit: An Unexpected Journey",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : "A reluctant hobbit, Bilbo Baggins, sets out to the Lonely Mountain with a spirited group of dwarves to reclaim their mountain home - and the gold within it - from the dragon Smaug."
}
{
"_id" : ObjectId("62a81d49a0464c977fa8f2e7"),
"title" : "The Hobbit: The Battle of the Five Armies",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : "Bilbo and Company are forced to engage in a war against an array of combatants and keep the the lonely mountains from falling into the hands of a rising darkness."
}
> db.movies.find({"$or":[{"synopsis":{"$regex":"dwarves"}},{"synopsis":{"$regex":"hobbit"}}]}).pretty()
{
"_id" : ObjectId("62a81d49a0464c977fa8f2e5"),
"title" : "The Hobbit: An Unexpected Journey",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : "A reluctant hobbit, Bilbo Baggins, sets out to the Lonely Mountain with a spirited group of dwarves to reclaim their mountain home - and the gold within it - from the dragon Smaug."
}
{
"_id" : ObjectId("62a81d49a0464c977fa8f2e6"),
"title" : "The Hobbit: The Desolation of Smaug",
"writer" : "J.R.R. Tolkein",
"year" : 2013,
"franchise" : "The Hobbit",
"synopsis" : "The dwarves, along with Bilbo Baggins and Gandalf the Grey, continue their quest to reclaim Erebor, their homeland, from Smaug. Bilbo Baggins is in possession of a mysterious and magical ring."
}
> db.movies.find({"$and":[{"synopsis":{"$regex":"gold"}},{"synopsis":{"$regex":"dragon"}}]}).pretty()
{
"_id" : ObjectId("62a81d49a0464c977fa8f2e5"),
"title" : "The Hobbit: An Unexpected Journey",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : "A reluctant hobbit, Bilbo Baggins, sets out to the Lonely Mountain with a spirited group of dwarves to reclaim their mountain home - and the gold within it - from the dragon Smaug."
}
> db.movies.deleteOne({"title":"Pee Wee Herman's Big Adventure"})
{ "acknowledged" : true, "deletedCount" : 1 }
> db.movies.find({},{_id:0,title:1}).pretty()
{ "title" : "Fight Club" }
{ "title" : "Pulp Fiction" }
{ "title" : "Inglorius Basterds" }
{ "title" : "The Hobbit: An Unexpected Journey" }
{ "title" : "The Hobbit: The Desolation of Smaug" }
{ "title" : "The Hobbit: The Battle of the Five Armies" }
{ "title" : "Avatar" }
> db.movies.deleteOne({"title":"Avatar"})
{ "acknowledged" : true, "deletedCount" : 1 }
> db.movies.find({},{_id:0,title:1}).pretty()
{ "title" : "Fight Club" }
{ "title" : "Pulp Fiction" }
{ "title" : "Inglorius Basterds" }
{ "title" : "The Hobbit: An Unexpected Journey" }
{ "title" : "The Hobbit: The Desolation of Smaug" }
{ "title" : "The Hobbit: The Battle of the Five Armies" }
> db.users.insertMany([{"username":"GoodGuyGreg","first_name":"Good Guy", "last_name" : "Greg"},{ "username" : "ScumbagSteve", "full_name" : {"first" : "Scumbag" , "last" : "Steve"}}])
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("62aed6f72965e0d6d04e8020"),
ObjectId("62aed6f72965e0d6d04e8021")
]
}
> db.users.find().pretty()
{
"_id" : ObjectId("62aed6f72965e0d6d04e8020"),
"username" : "GoodGuyGreg",
"first_name" : "Good Guy",
"last_name" : "Greg"
}
{
"_id" : ObjectId("62aed6f72965e0d6d04e8021"),
"username" : "ScumbagSteve",
"full_name" : {
"first" : "Scumbag",
"last" : "Steve"
}
}
> db.posts.insertMany([{"username":"GoodGuyGreg","title" :" Passes out at party", "body" :" Wakes up early and cleans house"},{"username":"GoodGuyGreg","title" :"Steals your identity", "body" :"Raises your credit score"},{"username":"GoodGuyGreg","title" :" Reports a bug in your code", "body" :" Sends you a Pull Request"},{"username":"ScumbagSteve","title" :" Borrows something", "body" :"Sells it"},{"username":"ScumbagSteve","title" :"Borrows everything", "body" :"The end"},{"username":"ScumbagSteve","title" :"Forks your repo on github", "body" :"Sets to private"}])
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("62aedb802965e0d6d04e8022"),
ObjectId("62aedb802965e0d6d04e8023"),
ObjectId("62aedb802965e0d6d04e8024"),
ObjectId("62aedb802965e0d6d04e8025"),
ObjectId("62aedb802965e0d6d04e8026"),
ObjectId("62aedb802965e0d6d04e8027")
]
}
> db.users.find().pretty()
{
"_id" : ObjectId("62aed6f72965e0d6d04e8020"),
"username" : "GoodGuyGreg",
"first_name" : "Good Guy",
"last_name" : "Greg"
}
{
"_id" : ObjectId("62aed6f72965e0d6d04e8021"),
"username" : "ScumbagSteve",
"full_name" : {
"first" : "Scumbag",
"last" : "Steve"
}
}
> db.posts.find().pretty()
{
"_id" : ObjectId("62aedb802965e0d6d04e8022"),
"username" : "GoodGuyGreg",
"title" : " Passes out at party",
"body" : " Wakes up early and cleans house"
}
{
"_id" : ObjectId("62aedb802965e0d6d04e8023"),
"username" : "GoodGuyGreg",
"title" : "Steals your identity",
"body" : "Raises your credit score"
}
{
"_id" : ObjectId("62aedb802965e0d6d04e8024"),
"username" : "GoodGuyGreg",
"title" : " Reports a bug in your code",
"body" : " Sends you a Pull Request"
}
{
"_id" : ObjectId("62aedb802965e0d6d04e8025"),
"username" : "ScumbagSteve",
"title" : " Borrows something",
"body" : "Sells it"
}
{
"_id" : ObjectId("62aedb802965e0d6d04e8026"),
"username" : "ScumbagSteve",
"title" : "Borrows everything",
"body" : "The end"
}
{
"_id" : ObjectId("62aedb802965e0d6d04e8027"),
"username" : "ScumbagSteve",
"title" : "Forks your repo on github",
"body" : "Sets to private"
}
> db.posts.find({username:"GoodGuyGreg"}).pretty()
{
"_id" : ObjectId("62aedb802965e0d6d04e8022"),
"username" : "GoodGuyGreg",
"title" : " Passes out at party",
"body" : " Wakes up early and cleans house"
}
{
"_id" : ObjectId("62aedb802965e0d6d04e8023"),
"username" : "GoodGuyGreg",
"title" : "Steals your identity",
"body" : "Raises your credit score"
}
{
"_id" : ObjectId("62aedb802965e0d6d04e8024"),
"username" : "GoodGuyGreg",
"title" : " Reports a bug in your code",
"body" : " Sends you a Pull Request"
}
> db.posts.find({username:"ScumbagSteve"}).pretty()
{
"_id" : ObjectId("62aedb802965e0d6d04e8025"),
"username" : "ScumbagSteve",
"title" : " Borrows something",
"body" : "Sells it"
}
{
"_id" : ObjectId("62aedb802965e0d6d04e8026"),
"username" : "ScumbagSteve",
"title" : "Borrows everything",
"body" : "The end"
}
{
"_id" : ObjectId("62aedb802965e0d6d04e8027"),
"username" : "ScumbagSteve",
"title" : "Forks your repo on github",
"body" : "Sets to private"
} |
Query / Find Documents
query the movies collection to
1. get all documents
db.movies.find().pretty();{
"_id" : ObjectId("62adbe75fcfadd26599efc94"),
"title" : "Fight Club",
"writer" : "Chuck Palahniuk",
"year" : 1999,
"actors" : [
"Brad Pitt",
"EEdward Norton"
]
}
{
"_id" : ObjectId("62adbe75fcfadd26599efc95"),
"title" : "Pulp Fiction",
"writer" : "Quentin Tarantino",
"year" : 1994,
"actors" : [
"John Travolta",
"Uma Thurman"
],
"actor" : "Samuel L. Jackson"
}
{
"_id" : ObjectId("62adbe75fcfadd26599efc96"),
"title" : "Inglorius Basterds",
"writer" : "Quentin Tarantino",
"year" : 2009,
"actors" : [
"Brad Pitt",
"Diane Kruger",
"Eli Roth"
]
}
{
"_id" : ObjectId("62adbe75fcfadd26599efc97"),
"title" : "The Hobbit: An Unexpected Journey",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : " A reluctant hobbit, Bilbo Baggins, sets out to the Lonely Mountain with a spirited group of dwarves to reclaim their mountain home - and the gold within it - from the dragon Smaug."
}
{
"_id" : ObjectId("62adbe75fcfadd26599efc98"),
"title" : "The Hobbit: The Desolation of smug",
"writer" : "J.R.R. Tolkein",
"year" : 2013,
"franchise" : "The Hobbit",
"synopsis" : "The dwarves, along with Bilbo Baggins and Gandalf the Grey, continue their quest to reclaim Erebor, their homeland, from Smaug. Bilbo Baggins is in possession of a mysterious and magical ring."
}
{
"_id" : ObjectId("62adbe75fcfadd26599efc99"),
"title" : "The Hobbit: The Battle of the Five Armies",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : "Bilbo and Company are forced to engage in a war against an array of combatants and keep the the lonely mountains from falling into the hands of a rising darkness."
}
2. get all documents with writer set to "Quentin Tarantino"
> db.movies.find({writer:"Quentin Tarantino"}).pretty();
{
"_id" : ObjectId("62adbe75fcfadd26599efc95"),
"title" : "Pulp Fiction",
"writer" : "Quentin Tarantino",
"year" : 1994,
"actors" : [
"John Travolta",
"Uma Thurman"
],
"actor" : "Samuel L. Jackson"
}
{
"_id" : ObjectId("62adbe75fcfadd26599efc96"),
"title" : "Inglorius Basterds",
"writer" : "Quentin Tarantino",
"year" : 2009,
"actors" : [
"Brad Pitt",
"Diane Kruger",
"Eli Roth"
]
}
3. get all documents where actors include "Brad Pitt"
> db.movies.find({actors: "Brad Pitt"}).pretty();
{
"_id" : ObjectId("62adbe75fcfadd26599efc94"),
"title" : "Fight Club",
"writer" : "Chuck Palahniuk",
"year" : 1999,
"actors" : [
"Brad Pitt",
"EEdward Norton"
]
}
{
"_id" : ObjectId("62adbe75fcfadd26599efc96"),
"title" : "Inglorius Basterds",
"writer" : "Quentin Tarantino",
"year" : 2009,
"actors" : [
"Brad Pitt",
"Diane Kruger",
"Eli Roth"
]
4. get all documents with franchise set to "The Hobbit"
> db.movies.find({franchise : "The Hobbit"}).pretty();
{
"_id" : ObjectId("62adbe75fcfadd26599efc97"),
"title" : "The Hobbit: An Unexpected Journey",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : " A reluctant hobbit, Bilbo Baggins, sets out to the Lonely Mountain with a spirited group of dwarves to reclaim their mountain home - and the gold within it - from the dragon Smaug."
}
{
"_id" : ObjectId("62adbe75fcfadd26599efc98"),
"title" : "The Hobbit: The Desolation of smug",
"writer" : "J.R.R. Tolkein",
"year" : 2013,
"franchise" : "The Hobbit",
"synopsis" : "The dwarves, along with Bilbo Baggins and Gandalf the Grey, continue their quest to reclaim Erebor, their homeland, from Smaug. Bilbo Baggins is in possession of a mysterious and magical ring."
}
{
"_id" : ObjectId("62adbe75fcfadd26599efc99"),
"title" : "The Hobbit: The Battle of the Five Armies",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : "Bilbo and Company are forced to engage in a war against an array of combatants and keep the the lonely mountains from falling into the hands of a rising darkness."
}
5. get all movies released in the 90s
> db. movies.find({year : { $gt : 1989, $lt : 2000}}).pretty();
{
"_id" : ObjectId("62adbe75fcfadd26599efc94"),
"title" : "Fight Club",
"writer" : "Chuck Palahniuk",
"year" : 1999,
"actors" : [
"Brad Pitt",
"EEdward Norton"
]
}
{
"_id" : ObjectId("62adbe75fcfadd26599efc95"),
"title" : "Pulp Fiction",
"writer" : "Quentin Tarantino",
"year" : 1994,
"actors" : [
"John Travolta",
"Uma Thurman"
],
"actor" : "Samuel L. Jackson"
}
6. get all movies released before the year 2000 or after 2010
> db.movies.find({$or:[{"year":{"$gt":2010}},{"year":{"$lt":2000}}]}).pretty()
{
"_id" : ObjectId("62adbe75fcfadd26599efc94"),
"title" : "Fight Club",
"writer" : "Chuck Palahniuk",
"year" : 1999,
"actors" : [
"Brad Pitt",
"EEdward Norton"
]
}
{
"_id" : ObjectId("62adbe75fcfadd26599efc95"),
"title" : "Pulp Fiction",
"writer" : "Quentin Tarantino",
"year" : 1994,
"actors" : [
"John Travolta",
"Uma Thurman"
],
"actor" : "Samuel L. Jackson"
}
{
"_id" : ObjectId("62adbe75fcfadd26599efc97"),
"title" : "The Hobbit: An Unexpected Journey",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : " A reluctant hobbit, Bilbo Baggins, sets out to the Lonely Mountain with a spirited group of dwarves to reclaim their mountain home - and the gold within it - from the dragon Smaug."
}
{
"_id" : ObjectId("62adbe75fcfadd26599efc98"),
"title" : "The Hobbit: The Desolation of smug",
"writer" : "J.R.R. Tolkein",
"year" : 2013,
"franchise" : "The Hobbit",
"synopsis" : "The dwarves, along with Bilbo Baggins and Gandalf the Grey, continue their quest to reclaim Erebor, their homeland, from Smaug. Bilbo Baggins is in possession of a mysterious and magical ring."
}
{
"_id" : ObjectId("62adbe75fcfadd26599efc99"),
"title" : "The Hobbit: The Battle of the Five Armies",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : "Bilbo and Company are forced to engage in a war against an array of combatants and keep the the lonely mountains from falling into the hands of a rising darkness."
}
Update Documents
1. add a synopsis to "The Hobbit: An Unexpected Journey" : "A reluctant hobbit, Bilbo Baggins, sets out to the Lonely Mountain with a spirited group of dwarves to reclaim their mountain home - and the gold within it - from the dragon Smaug."
db.movies.updateOne(
{title: "The Hobbit: An Unexpected Journey"},
{
$set: {"synopsis": " A reluctant hobbit, Bilbo Baggins, sets out to the Lonely Mountain with a spirited group of dwarves to reclaim their mountain home - and the gold within it - from the dragon Smaug."}
});
{
"_id" : ObjectId("62adbe75fcfadd26599efc97"),
"title" : "The Hobbit: An Unexpected Journey",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : " A reluctant hobbit, Bilbo Baggins, sets out to the Lonely Mountain with a spirited group of dwarves to reclaim their mountain home - and the gold within it - from the dragon Smaug."
}
2. add a synopsis to "The Hobbit: The Desolation of Smaug" : "The dwarves, along with Bilbo Baggins and Gandalf the Grey, continue their quest to reclaim Erebor, their homeland, from Smaug. Bilbo Baggins is in possession of a mysterious and magical ring."
db.movies.updateOne({title: "The Hobbit: The Desolation of smug"},{$set:{"synopsis":"The dwarves, along with Bilbo Baggins and Gandalf the Grey, continue their quest to reclaim Erebor, their homeland, from Smaug. Bilbo Baggins is in possession of a mysterious and magical ring."}});
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
3. add an actor named "Samuel L. Jackson" to the movie "Pulp Fiction"
db.movies.updateOne({title: "Pulp Fiction"},{$set: {"actor": "Samuel L. Jackson"}});
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }Text Search
1. find all movies that have a synopsis that contains the word "Bilbo"
db.movies.find({"synopsis" : /.*Bilbo.*/i});
{ "_id" : ObjectId("62adbe75fcfadd26599efc97"), "title" : "The Hobbit: An Unexpected Journey", "writer" : "J.R.R. Tolkein", "year" : 2012, "franchise" : "The Hobbit", "synopsis" : " A reluctant hobbit, Bilbo Baggins, sets out to the Lonely Mountain with a spirited group of dwarves to reclaim their mountain home - and the gold within it - from the dragon Smaug." }
{ "_id" : ObjectId("62adbe75fcfadd26599efc98"), "title" : "The Hobbit: The Desolation of smug", "writer" : "J.R.R. Tolkein", "year" : 2013, "franchise" : "The Hobbit", "synopsis" : "The dwarves, along with Bilbo Baggins and Gandalf the Grey, continue their quest to reclaim Erebor, their homeland, from Smaug. Bilbo Baggins is in possession of a mysterious and magical ring." }
{ "_id" : ObjectId("62adbe75fcfadd26599efc99"), "title" : "The Hobbit: The Battle of the Five Armies", "writer" : "J.R.R. Tolkein", "year" : 2012, "franchise" : "The Hobbit", "synopsis" : "Bilbo and Company are forced to engage in a war against an array of combatants and keep the the lonely mountains from falling into the hands of a rising darkness." }
2. find all movies that have a synopsis that contains the word "Gandalf"
db.movies.find({ "synopsis": { $regex: ".* Gandalf.*" } }).pretty()
{
"_id" : ObjectId("62adbe75fcfadd26599efc98"),
"title" : "The Hobbit: The Desolation of smug",
"writer" : "J.R.R. Tolkein",
"year" : 2013,
"franchise" : "The Hobbit",
"synopsis" : "The dwarves, along with Bilbo Baggins and Gandalf the Grey, continue their quest to reclaim Erebor, their homeland, from Smaug. Bilbo Baggins is in possession of a mysterious and magical ring."
3. find all movies that have a synopsis that contains the word "Bilbo" and not the word "Gandalf"
db.movies.find({"synopsis":{"$regex":".*Bilbo.*",$not:{"$regex":".* Gandalf.*"}}}).pretty()
{
"_id" : ObjectId("62adbe75fcfadd26599efc97"),
"title" : "The Hobbit: An Unexpected Journey",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : " A reluctant hobbit, Bilbo Baggins, sets out to the Lonely Mountain with a spirited group of dwarves to reclaim their mountain home - and the gold within it - from the dragon Smaug."
}
{
"_id" : ObjectId("62adbe75fcfadd26599efc99"),
"title" : "The Hobbit: The Battle of the Five Armies",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : "Bilbo and Company are forced to engage in a war against an array of combatants and keep the the lonely mountains from falling into the hands of a rising darkness."
4. find all movies that have a synopsis that contains the word "dwarves" or "hobbit"
db.movies.find( { $or: [ { synopsis: { "$regex":".* dwarves.*"} }, {"synopsis":{"$regex":".* hobbit.*"}} ] } ).pretty();
{
"_id" : ObjectId("62adbe75fcfadd26599efc97"),
"title" : "The Hobbit: An Unexpected Journey",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : " A reluctant hobbit, Bilbo Baggins, sets out to the Lonely Mountain with a spirited group of dwarves to reclaim their mountain home - and the gold within it - from the dragon Smaug."
}
{
"_id" : ObjectId("62adbe75fcfadd26599efc98"),
"title" : "The Hobbit: The Desolation of smug",
"writer" : "J.R.R. Tolkein",
"year" : 2013,
"franchise" : "The Hobbit",
"synopsis" : "The dwarves, along with Bilbo Baggins and Gandalf the Grey, continue their quest to reclaim Erebor, their homeland, from Smaug. Bilbo Baggins is in possession of a mysterious and magical ring."
}
find all movies that have a synopsis that contains the word "gold" and "dragon"
db.movies.find( { $and: [ { synopsis: { "$regex":".* gold.*"} }, {"synopsis":{"$regex":".* dragon.*"}} ] } ).pretty();
{
"_id" : ObjectId("62adbe75fcfadd26599efc97"),
"title" : "The Hobbit: An Unexpected Journey",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : " A reluctant hobbit, Bilbo Baggins, sets out to the Lonely Mountain with a spirited group of dwarves to reclaim their mountain home - and the gold within it - from the dragon Smaug."
**Delete Documents
1. delete the movie "Pee Wee Herman's Big Adventure"**
db.movies.deleteOne({ title : "Pee Wee Herman\'s Big Adventure" })
{ "acknowledged" : true, "deletedCount" : 1 }
**2. delete the movie "Avatar"**
db.movies.deleteOne({ title : "Avatar" })
{ "acknowledged" : true, "deletedCount" : 1 } Relationships
**Insert the following documents into a users collection**
username : GoodGuyGreg
first_name : "Good Guy"
last_name : "Greg"
username : ScumbagSteve
full_name :
first : "Scumbag"
last : "Steve"
> db.users.insertOne(
... {"username" : "ScumbagSteve",
... "full_name" : {"first" : "Scumbag" , "last" : "Steve"}
... }
... );
{
"acknowledged" : true,
"insertedId" : ObjectId("62b2e4fa52a3bd962673d9de")
> db.users.find().pretty();
{
"_id" : ObjectId("62b2e0b2bb9653361ad77c3a"),
"username" : "GoodGuyGreg",
"first_name" : "Good Guy",
"last_name" : "Greg"
}
{
"_id" : ObjectId("62b2e4fa52a3bd962673d9de"),
"username" : "ScumbagSteve",
"full_name" : {
"first" : "Scumbag",
"last" : "Steve"
}
}
**Insert the following documents into a posts collection**
db.createCollection("posts");
{ "ok" : 1 }
username : GoodGuyGreg
title : Passes out at party
body : Wakes up early and cleans house
username : GoodGuyGreg
title : Steals your identity
body : Raises your credit score
username : GoodGuyGreg
title : Reports a bug in your code
body : Sends you a Pull Request
username : ScumbagSteve
title : Borrows something
body : Sells it
username : ScumbagSteve
title : Borrows everything
body : The end
username : ScumbagSteve
title : Forks your repo on github
body : Sets to private
> db.posts.insertMany([{
... username : "GoodGuyGreg",
... title : "Steals your identity",
... body : "Raises your credit score"
... },
... {
... username : "GoodGuyGreg",
... title : "Reports a bug in your code",
... body : "Sends you a Pull Request"
... },
... {
... username : "ScumbagSteve",
... title : "Borrows something",
... body : "Sells it"
... },
... {
... username : "ScumbagSteve",
... title : "Borrows everything",
... body : "The end"
... },
... {username : "ScumbagSteve",
... title : "Forks your repo on github",
... body : "Sets to private"
... }]);
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("62b2eab252a3bd962673d9e0"),
ObjectId("62b2eab252a3bd962673d9e1"),
ObjectId("62b2eab252a3bd962673d9e2"),
ObjectId("62b2eab252a3bd962673d9e3"),
ObjectId("62b2eab252a3bd962673d9e4")
]
}
**Insert the following documents into a comments collection**
> db.createCollection("comments");
{ "ok" : 1 }
> show collections
comments
movies
posts
stores
users
username : GoodGuyGreg
comment : Hope you got a good deal!
post : [post_obj_id]
where [post_obj_id] is the ObjectId of the posts document: "Borrows something"
username : GoodGuyGreg
comment : What's mine is yours!
post : [post_obj_id]
where [post_obj_id] is the ObjectId of the posts document: "Borrows everything"
username : GoodGuyGreg
comment : Don't violate the licensing agreement!
post : [post_obj_id]
where [post_obj_id] is the ObjectId of the posts document: "Forks your repo on github
username : ScumbagSteve
comment : It still isn't clean
post : [post_obj_id]
where [post_obj_id] is the ObjectId of the posts document: "Passes out at party"
username : ScumbagSteve
comment : Denied your PR cause I found a hack
post : [post_obj_id]
where [post_obj_id] is the ObjectId of the posts document: "Reports a bug in your code"
db.comments.insertMany(
[{
username : "GoodGuyGreg",
comment : "Hope you got a good deal!",
post : ["62b2eab252a3bd962673d9e2"]
},
{
username : "GoodGuyGreg",
comment : "What's mine is yours! ",
post : ["62b2eab252a3bd962673d9e3"]
},
{
username : "GoodGuyGreg",
comment : "Don't violate the licensing agreement! ",
post : ["62b2eab252a3bd962673d9e4"]
},
{
username : "ScumbagSteve",
comment : "It still isn\'t clean",
post : ["62b2e8d052a3bd962673d9df"],
},
{
username : "ScumbagSteve",
comment : "Denied your PR cause I found a hack",
post : ["62b2eab252a3bd962673d9e1"]
}])
Querying related collections
1. **find all users**
db.users.find().pretty();
{
"_id" : ObjectId("62b2e0b2bb9653361ad77c3a"),
"username" : "GoodGuyGreg",
"first_name" : "Good Guy",
"last_name" : "Greg"
}
{
"_id" : ObjectId("62b2e4fa52a3bd962673d9de"),
"username" : "ScumbagSteve",
"full_name" : {
"first" : "Scumbag",
"last" : "Steve"
2. **find all posts**
> db.posts.find().pretty();
{
"_id" : ObjectId("62b2e8d052a3bd962673d9df"),
"username" : "GoodGuyGreg",
"title" : "Passes out at party",
"body" : "Wakes up early and cleans house"
}
{
"_id" : ObjectId("62b2eab252a3bd962673d9e0"),
"username" : "GoodGuyGreg",
"title" : "Steals your identity",
"body" : "Raises your credit score"
}
{
"_id" : ObjectId("62b2eab252a3bd962673d9e1"),
"username" : "GoodGuyGreg",
"title" : "Reports a bug in your code",
"body" : "Sends you a Pull Request"
}
{
"_id" : ObjectId("62b2eab252a3bd962673d9e2"),
"username" : "ScumbagSteve",
"title" : "Borrows something",
"body" : "Sells it"
}
{
"_id" : ObjectId("62b2eab252a3bd962673d9e3"),
"username" : "ScumbagSteve",
"title" : "Borrows everything",
"body" : "The end"
}
{
"_id" : ObjectId("62b2eab252a3bd962673d9e4"),
"username" : "ScumbagSteve",
"title" : "Forks your repo on github",
"body" : "S
3. **find all posts that was authored by "GoodGuyGreg"**
> db.posts.find({username:"GoodGuyGreg"}).pretty()
{
"_id" : ObjectId("62b2e8d052a3bd962673d9df"),
"username" : "GoodGuyGreg",
"title" : "Passes out at party",
"body" : "Wakes up early and cleans house"
}
{
"_id" : ObjectId("62b2eab252a3bd962673d9e0"),
"username" : "GoodGuyGreg",
"title" : "Steals your identity",
"body" : "Raises your credit score"
}
{
"_id" : ObjectId("62b2eab252a3bd962673d9e1"),
"username" : "GoodGuyGreg",
"title" : "Reports a bug in your code",
"body" : "Sends you a Pull Request"
4. **find all posts that was authored by "ScumbagSteve"**
> db.posts.find({username:"ScumbagSteve"}).pretty()
{
"_id" : ObjectId("62b2eab252a3bd962673d9e2"),
"username" : "ScumbagSteve",
"title" : "Borrows something",
"body" : "Sells it"
}
{
"_id" : ObjectId("62b2eab252a3bd962673d9e3"),
"username" : "ScumbagSteve",
"title" : "Borrows everything",
"body" : "The end"
}
{
"_id" : ObjectId("62b2eab252a3bd962673d9e4"),
"username" : "ScumbagSteve",
"title" : "Forks your repo on github",
"body" : "Sets to private"
}
**5. find all comments**
> db.comments.find().pretty();
{
"_id" : ObjectId("62b306e552a3bd962673d9e5"),
"username" : "GoodGuyGreg",
"comment" : "Hope you got a good deal!",
"post" : [
"62b2eab252a3bd962673d9e2"
]
}
{
"_id" : ObjectId("62b306e552a3bd962673d9e6"),
"username" : "GoodGuyGreg",
"comment" : "What's mine is yours! ",
"post" : [
"62b2eab252a3bd962673d9e3"
]
}
{
"_id" : ObjectId("62b306e552a3bd962673d9e7"),
"username" : "GoodGuyGreg",
"comment" : "Don't violate the licensing agreement! ",
"post" : [
"62b2eab252a3bd962673d9e4"
]
}
{
"_id" : ObjectId("62b306e552a3bd962673d9e8"),
"username" : "ScumbagSteve",
"comment" : "It still isn't clean",
"post" : [
"62b2e8d052a3bd962673d9df"
]
}
{
"_id" : ObjectId("62b306e552a3bd962673d9e9"),
"username" : "ScumbagSteve",
"comment" : "Denied your PR cause I found a hack",
"post" : [
"62b2eab252a3bd962673d9e1"
]
}
**6. find all comments that was authored by "GoodGuyGreg"**
> db.comments.find({username:"GoodGuyGreg"}).pretty()
{
"_id" : ObjectId("62b306e552a3bd962673d9e5"),
"username" : "GoodGuyGreg",
"comment" : "Hope you got a good deal!",
"post" : [
"62b2eab252a3bd962673d9e2"
]
}
{
"_id" : ObjectId("62b306e552a3bd962673d9e6"),
"username" : "GoodGuyGreg",
"comment" : "What's mine is yours! ",
"post" : [
"62b2eab252a3bd962673d9e3"
]
}
{
"_id" : ObjectId("62b306e552a3bd962673d9e7"),
"username" : "GoodGuyGreg",
"comment" : "Don't violate the licensing agreement! ",
"post" : [
"62b2eab252a3bd962673d9e4"
]
}
**7. find all comments that was authored by "ScumbagSteve"**
> db.comments.find({username:"GoodGuyGreg"}).pretty()
{
"_id" : ObjectId("62b306e552a3bd962673d9e5"),
"username" : "GoodGuyGreg",
"comment" : "Hope you got a good deal!",
"post" : [
"62b2eab252a3bd962673d9e2"
]
}
{
"_id" : ObjectId("62b306e552a3bd962673d9e6"),
"username" : "GoodGuyGreg",
"comment" : "What's mine is yours! ",
"post" : [
"62b2eab252a3bd962673d9e3"
]
}
{
"_id" : ObjectId("62b306e552a3bd962673d9e7"),
"username" : "GoodGuyGreg",
"comment" : "Don't violate the licensing agreement! ",
"post" : [
"62b2eab252a3bd962673d9e4"
]
}
> db.comments.find({username:"ScumbagSteve"}).pretty()
{
"_id" : ObjectId("62b306e552a3bd962673d9e8"),
"username" : "ScumbagSteve",
"comment" : "It still isn't clean",
"post" : [
"62b2e8d052a3bd962673d9df"
]
}
{
"_id" : ObjectId("62b306e552a3bd962673d9e9"),
"username" : "ScumbagSteve",
"comment" : "Denied your PR cause I found a hack",
"post" : [
"62b2eab252a3bd962673d9e1"
]
}
|
CODERELATIONSHIP BETWEEN COLLECTIONS |
|
Insert Documents show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
mongo_sample 0.000GB
testDb 0.000GB
> use mongo_practice
switched to db mongo_practice
>db.movies.insertMany([{title: "Fight Club", writer: "Chuck Palahniuk", year: 1999, actors: ["Brad Pitt", "EEdward Norton"]},
{title: "Pulp Fiction", writer: "Quentin Tarantino", year: 1994, actors: ["John Travolta", "Uma Thurman"]},
{title: "Inglorius Basterds", writer: "Quentin Tarantino", year: 2009, actors: ["Brad Pitt", "Diane Kruger", "Eli Roth"]},
{title: "The Hobbit: An Unexpected Journey", writer: "J.R.R. Tolkein", year: 2012, franchise: "The Hobbit"},
{title: "The Hobbit: The Desolation of smug", writer: "J.R.R. Tolkein", year: 2013, franchise: "The Hobbit"},
{title: "The Hobbit: The Battle of the Five Armies", writer: "J.R.R. Tolkein", year: 2012, franchise: "The Hobbit",
synopsis: "Bilbo and Company are forced to engage in a war against an array of combatants and keep the the lonely mountains from falling into the hands of a rising darkness."},
{title: "Pee Wee Herman's Big Adventure"}, {title: "Avatar"}]);
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("62b581a2b69d4c59dd461c67"),
ObjectId("62b581a2b69d4c59dd461c68"),
ObjectId("62b581a2b69d4c59dd461c69"),
ObjectId("62b581a2b69d4c59dd461c6a"),
ObjectId("62b581a2b69d4c59dd461c6b"),
ObjectId("62b581a2b69d4c59dd461c6c"),
ObjectId("62b581a2b69d4c59dd461c6d"),
ObjectId("62b581a2b69d4c59dd461c6e")
]
}Query / Find Documents //1.get all documents
> db.movies.find({}).pretty();
{
"_id" : ObjectId("62b581a2b69d4c59dd461c67"),
"title" : "Fight Club",
"writer" : "Chuck Palahniuk",
"year" : 1999,
"actors" : [
"Brad Pitt",
"EEdward Norton"
]
}
{
"_id" : ObjectId("62b581a2b69d4c59dd461c68"),
"title" : "Pulp Fiction",
"writer" : "Quentin Tarantino",
"year" : 1994,
"actors" : [
"John Travolta",
"Uma Thurman"
]
}
{
"_id" : ObjectId("62b581a2b69d4c59dd461c69"),
"title" : "Inglorius Basterds",
"writer" : "Quentin Tarantino",
"year" : 2009,
"actors" : [
"Brad Pitt",
"Diane Kruger",
"Eli Roth"
]
}
{
"_id" : ObjectId("62b581a2b69d4c59dd461c6a"),
"title" : "The Hobbit: An Unexpected Journey",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit"
}
{
"_id" : ObjectId("62b581a2b69d4c59dd461c6b"),
"title" : "The Hobbit: The Desolation of smug",
"writer" : "J.R.R. Tolkein",
"year" : 2013,
"franchise" : "The Hobbit"
}
{
"_id" : ObjectId("62b581a2b69d4c59dd461c6c"),
"title" : "The Hobbit: The Battle of the Five Armies",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : "Bilbo and Company are forced to engage in a war against an array of combatants and keep the the lonely mountains from falling into the hands of a rising darkness."
}
{
"_id" : ObjectId("62b581a2b69d4c59dd461c6d"),
"title" : "Pee Wee Herman's Big Adventure"
}
{ "_id" : ObjectId("62b581a2b69d4c59dd461c6e"), "title" : "Avatar" }
//2.get all documents with writer set to "Quentin Tarantino"
> db.movies.find( { writer : { $eq : "Quentin Tarantino" }}).pretty();
{
"_id" : ObjectId("62a78b4b7960f45ec00b89bc"),
"title" : "Pulp Fiction",
"writer" : "Quentin Tarantino",
"year" : 1994,
"actors" : [
"John Travolta",
"Uma Thurman"
]
}
{
"_id" : ObjectId("62a78b4b7960f45ec00b89bd"),
"title" : "Inglorius Basterds",
"writer" : "Quentin Tarantino",
"year" : 2009,
"actors" : [
"Brad Pitt",
"Diane Kruger",
"Eli Roth"
]
}
//3.get all documents where actors include "Brad Pitt"
> db.movies.find( { actors : { $in : ["Brad Pitt"]}}).pretty();
{
"_id" : ObjectId("62b581a2b69d4c59dd461c67"),
"title" : "Fight Club",
"writer" : "Chuck Palahniuk",
"year" : 1999,
"actors" : [
"Brad Pitt",
"EEdward Norton"
]
}
{
"_id" : ObjectId("62b581a2b69d4c59dd461c69"),
"title" : "Inglorius Basterds",
"writer" : "Quentin Tarantino",
"year" : 2009,
"actors" : [
"Brad Pitt",
"Diane Kruger",
"Eli Roth"
]
}
//4.get all documents with franchise set to "The Hobbit"
> db.movies.find( { franchise : { $eq : "The Hobbit" }}).pretty();
{
"_id" : ObjectId("62b581a2b69d4c59dd461c6a"),
"title" : "The Hobbit: An Unexpected Journey",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit"
}
{
"_id" : ObjectId("62b581a2b69d4c59dd461c6b"),
"title" : "The Hobbit: The Desolation of smug",
"writer" : "J.R.R. Tolkein",
"year" : 2013,
"franchise" : "The Hobbit"
}
{
"_id" : ObjectId("62b581a2b69d4c59dd461c6c"),
"title" : "The Hobbit: The Battle of the Five Armies",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : "Bilbo and Company are forced to engage in a war against an array of combatants and keep the the lonely mountains from falling into the hands of a rising darkness."
}
//5.get all movies released in the 90s
> db.movies.find({ $and : [ {year : { $gte :1990 }}, { year :{ $lte : 1999 }} ] }).pretty();
{
"_id" : ObjectId("62b581a2b69d4c59dd461c67"),
"title" : "Fight Club",
"writer" : "Chuck Palahniuk",
"year" : 1999,
"actors" : [
"Brad Pitt",
"EEdward Norton"
]
}
{
"_id" : ObjectId("62b581a2b69d4c59dd461c68"),
"title" : "Pulp Fiction",
"writer" : "Quentin Tarantino",
"year" : 1994,
"actors" : [
"John Travolta",
"Uma Thurman"
]
}
//6.get all movies released before the year 2000 or after 2010
> db.movies.find({ $or : [ {year : { $gt :2010 }}, { year :{ $lt : 2000 }} ] }).pretty();
{
"_id" : ObjectId("62b581a2b69d4c59dd461c67"),
"title" : "Fight Club",
"writer" : "Chuck Palahniuk",
"year" : 1999,
"actors" : [
"Brad Pitt",
"EEdward Norton"
]
}
{
"_id" : ObjectId("62b581a2b69d4c59dd461c68"),
"title" : "Pulp Fiction",
"writer" : "Quentin Tarantino",
"year" : 1994,
"actors" : [
"John Travolta",
"Uma Thurman"
]
}
{
"_id" : ObjectId("62b581a2b69d4c59dd461c6a"),
"title" : "The Hobbit: An Unexpected Journey",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit"
}
{
"_id" : ObjectId("62b581a2b69d4c59dd461c6b"),
"title" : "The Hobbit: The Desolation of smug",
"writer" : "J.R.R. Tolkein",
"year" : 2013,
"franchise" : "The Hobbit"
}
{
"_id" : ObjectId("62b581a2b69d4c59dd461c6c"),
"title" : "The Hobbit: The Battle of the Five Armies",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : "Bilbo and Company are forced to engage in a war against an array of combatants and keep the the lonely mountains from falling into the hands of a rising darkness."
}Update Documents //1.add a synopsis to "The Hobbit: An Unexpected Journey" : "A reluctant hobbit, Bilbo Baggins, sets out to the Lonely Mountain with a spirited group of dwarves to reclaim their mountain home - and the gold within it - from the dragon Smaug."
> db.movies.updateOne({"title":"The Hobbit: An Unexpected Journey"},{$set:{"synopsis":"A reluctant hobbit, Bilbo Baggins, sets out to the Lonely Mountain with a spirited group of dwarves to reclaim their mountain home - and the gold within it - from the dragon Smaug."}});
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
//2.add a synopsis to "The Hobbit: The Desolation of Smaug" : "The dwarves, along with Bilbo Baggins and Gandalf the Grey, continue their quest to reclaim Erebor, their homeland, from Smaug. Bilbo Baggins is in possession of a mysterious and magical ring."
> db.movies.updateOne({"title":"The Hobbit: The Desolation of smug"},{$set:{"synopsis":"The dwarves, along with Bilbo Baggins and Gandalf the Grey, continue their quest to reclaim Erebor, their homeland, from Smaug. Bilbo Baggins is in possession of a mysterious and magical ring."}});
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
//3.add an actor named "Samuel L. Jackson" to the movie "Pulp Fiction"
> db.movies.updateOne({"title":"Pulp Fiction"},{$push:{"actors":"Samuel L. Jackson"}});
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
> db.movies.find({}).pretty();
{
"_id" : ObjectId("62b581a2b69d4c59dd461c67"),
"title" : "Fight Club",
"writer" : "Chuck Palahniuk",
"year" : 1999,
"actors" : [
"Brad Pitt",
"EEdward Norton"
]
}
{
"_id" : ObjectId("62b581a2b69d4c59dd461c68"),
"title" : "Pulp Fiction",
"writer" : "Quentin Tarantino",
"year" : 1994,
"actors" : [
"John Travolta",
"Uma Thurman",
"Samuel L. Jackson"
]
}
{
"_id" : ObjectId("62b581a2b69d4c59dd461c69"),
"title" : "Inglorius Basterds",
"writer" : "Quentin Tarantino",
"year" : 2009,
"actors" : [
"Brad Pitt",
"Diane Kruger",
"Eli Roth"
]
}
{
"_id" : ObjectId("62b581a2b69d4c59dd461c6a"),
"title" : "The Hobbit: An Unexpected Journey",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : "A reluctant hobbit, Bilbo Baggins, sets out to the Lonely Mountain with a spirited group of dwarves to reclaim their mountain home - and the gold within it - from the dragon Smaug."
}
{
"_id" : ObjectId("62b581a2b69d4c59dd461c6b"),
"title" : "The Hobbit: The Desolation of smug",
"writer" : "J.R.R. Tolkein",
"year" : 2013,
"franchise" : "The Hobbit",
"synopsis" : "The dwarves, along with Bilbo Baggins and Gandalf the Grey, continue their quest to reclaim Erebor, their homeland, from Smaug. Bilbo Baggins is in possession of a mysterious and magical ring."
}
{
"_id" : ObjectId("62b581a2b69d4c59dd461c6c"),
"title" : "The Hobbit: The Battle of the Five Armies",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : "Bilbo and Company are forced to engage in a war against an array of combatants and keep the the lonely mountains from falling into the hands of a rising darkness."
}
{
"_id" : ObjectId("62b581a2b69d4c59dd461c6d"),
"title" : "Pee Wee Herman's Big Adventure"
}
{ "_id" : ObjectId("62b581a2b69d4c59dd461c6e"), "title" : "Avatar" }Text Search //1.find all movies that have a synopsis that contains the word "Bilbo"
> db.movies.find({"synopsis":{"$regex":"Bilbo"}}).pretty();
{
"_id" : ObjectId("62b581a2b69d4c59dd461c6a"),
"title" : "The Hobbit: An Unexpected Journey",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : "A reluctant hobbit, Bilbo Baggins, sets out to the Lonely Mountain with a spirited group of dwarves to reclaim their mountain home - and the gold within it - from the dragon Smaug."
}
{
"_id" : ObjectId("62b581a2b69d4c59dd461c6b"),
"title" : "The Hobbit: The Desolation of smug",
"writer" : "J.R.R. Tolkein",
"year" : 2013,
"franchise" : "The Hobbit",
"synopsis" : "The dwarves, along with Bilbo Baggins and Gandalf the Grey, continue their quest to reclaim Erebor, their homeland, from Smaug. Bilbo Baggins is in possession of a mysterious and magical ring."
}
{
"_id" : ObjectId("62b581a2b69d4c59dd461c6c"),
"title" : "The Hobbit: The Battle of the Five Armies",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : "Bilbo and Company are forced to engage in a war against an array of combatants and keep the the lonely mountains from falling into the hands of a rising darkness."
}
//2.find all movies that have a synopsis that contains the word "Gandalf"
> db.movies.find({"synopsis":{"$regex":"Gandalf"}}).pretty();
{
"_id" : ObjectId("62b581a2b69d4c59dd461c6b"),
"title" : "The Hobbit: The Desolation of smug",
"writer" : "J.R.R. Tolkein",
"year" : 2013,
"franchise" : "The Hobbit",
"synopsis" : "The dwarves, along with Bilbo Baggins and Gandalf the Grey, continue their quest to reclaim Erebor, their homeland, from Smaug. Bilbo Baggins is in possession of a mysterious and magical ring."
}
//3.find all movies that have a synopsis that contains the word "Bilbo" and not the word "Gandalf"
> db.movies.find({"synopsis":{"$regex":"Bilbo",$not:{"$regex":"Gandalf"}}}).pretty();
{
"_id" : ObjectId("62b581a2b69d4c59dd461c6a"),
"title" : "The Hobbit: An Unexpected Journey",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : "A reluctant hobbit, Bilbo Baggins, sets out to the Lonely Mountain with a spirited group of dwarves to reclaim their mountain home - and the gold within it - from the dragon Smaug."
}
{
"_id" : ObjectId("62b581a2b69d4c59dd461c6c"),
"title" : "The Hobbit: The Battle of the Five Armies",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : "Bilbo and Company are forced to engage in a war against an array of combatants and keep the the lonely mountains from falling into the hands of a rising darkness."
}
//4.find all movies that have a synopsis that contains the word "dwarves" or "hobbit"
> db.movies.find({"$or":[{"synopsis":{"$regex":"dwarves"}},{"synopsis":{"$regex":"hobbit"}}]}).pretty();
{
"_id" : ObjectId("62b581a2b69d4c59dd461c6a"),
"title" : "The Hobbit: An Unexpected Journey",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : "A reluctant hobbit, Bilbo Baggins, sets out to the Lonely Mountain with a spirited group of dwarves to reclaim their mountain home - and the gold within it - from the dragon Smaug."
}
{
"_id" : ObjectId("62b581a2b69d4c59dd461c6b"),
"title" : "The Hobbit: The Desolation of smug",
"writer" : "J.R.R. Tolkein",
"year" : 2013,
"franchise" : "The Hobbit",
"synopsis" : "The dwarves, along with Bilbo Baggins and Gandalf the Grey, continue their quest to reclaim Erebor, their homeland, from Smaug. Bilbo Baggins is in possession of a mysterious and magical ring."
}
//5.find all movies that have a synopsis that contains the word "gold" and "dragon"
> db.movies.find({"$and":[{"synopsis":{"$regex":"gold"}},{"synopsis":{"$regex":"dragon"}}]}).pretty();
{
"_id" : ObjectId("62b581a2b69d4c59dd461c6a"),
"title" : "The Hobbit: An Unexpected Journey",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : "A reluctant hobbit, Bilbo Baggins, sets out to the Lonely Mountain with a spirited group of dwarves to reclaim their mountain home - and the gold within it - from the dragon Smaug."
}Delete Documents //1.delete the movie "Pee Wee Herman's Big Adventure"
> db.movies.deleteOne({"title":"Pee Wee Herman's Big Adventure"});
{ "acknowledged" : true, "deletedCount" : 1 }
//2.delete the movie "Avatar"
> db.movies.deleteOne({"title":"Avatar"});
{ "acknowledged" : true, "deletedCount" : 1 }
> db.movies.find({},{_id:0,title:1}).pretty();
{ "title" : "Fight Club" }
{ "title" : "Pulp Fiction" }
{ "title" : "Inglorius Basterds" }
{ "title" : "The Hobbit: An Unexpected Journey" }
{ "title" : "The Hobbit: The Desolation of smug" }
{ "title" : "The Hobbit: The Battle of the Five Armies" }RelationshipsInsert the following documents into a users collection > db.users.insertMany([{"username":"GoodGuyGreg","first_name":"Good Guy", "last_name" : "Greg"},{ "username" : "ScumbagSteve", "full_name" : {"first" : "Scumbag" , "last" : "Steve"}}]);
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("62b6b48fe04e1b79cff3b4b8"),
ObjectId("62b6b48fe04e1b79cff3b4b9")
]
}Insert the following documents into a posts collection > db.posts.insertMany([{"username":"GoodGuyGreg","title" :" Passes out at party", "body" :" Wakes up early and cleans house"},{"username":"GoodGuyGreg","title" :"Steals your identity", "body" :"Raises your credit score"},{"username":"GoodGuyGreg","title" :" Reports a bug in your code", "body" :" Sends you a Pull Request"},{"username":"ScumbagSteve","title" :" Borrows something", "body" :"Sells it"},{"username":"ScumbagSteve","title" :"Borrows everything", "body" :"The end"},{"username":"ScumbagSteve","title" :"Forks your repo on github", "body" :"Sets to private"}]);
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("62b6b526e04e1b79cff3b4ba"),
ObjectId("62b6b526e04e1b79cff3b4bb"),
ObjectId("62b6b526e04e1b79cff3b4bc"),
ObjectId("62b6b526e04e1b79cff3b4bd"),
ObjectId("62b6b526e04e1b79cff3b4be"),
ObjectId("62b6b526e04e1b79cff3b4bf")
]
}Insert the following documents into a comments collection > db.comments.insertMany([{username: "GoodGuyGreg", comment: "Hope you got a good deal!", post: ObjectId("62b2131f58e2771bd28580f0")},{username: "GoodGuyGreg", comment: "Don't violate the licensing agreement!",post: ObjectId("62b2131f58e2771bd28580f1")},{ username: "GoodGuyGreg",comment: "Don't violate the licensing agreement!",post: ObjectId("62b2131f58e2771bd28580f2")},{ username: "ScumbagSteve", comment: "It still isn't clean", post: ObjectId("62b2131f58e2771bd28580ed")},{ username: "ScumbagSteve", comment: "Denied your PR cause I found a hack",post: ObjectId("62b2131f58e2771bd28580ef")}]);
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("62b6b82ae04e1b79cff3b4c0"),
ObjectId("62b6b82ae04e1b79cff3b4c1"),
ObjectId("62b6b82ae04e1b79cff3b4c2"),
ObjectId("62b6b82ae04e1b79cff3b4c3"),
ObjectId("62b6b82ae04e1b79cff3b4c4")
]
}Querying related collections//1.find all users
> db.users.find({}).pretty();
{
"_id" : ObjectId("62b6b48fe04e1b79cff3b4b8"),
"username" : "GoodGuyGreg",
"first_name" : "Good Guy",
"last_name" : "Greg"
}
{
"_id" : ObjectId("62b6b48fe04e1b79cff3b4b9"),
"username" : "ScumbagSteve",
"full_name" : {
"first" : "Scumbag",
"last" : "Steve"
}
}
//2.find all posts
> db.posts.find({}).pretty();
{
"_id" : ObjectId("62b6b526e04e1b79cff3b4ba"),
"username" : "GoodGuyGreg",
"title" : " Passes out at party",
"body" : " Wakes up early and cleans house"
}
{
"_id" : ObjectId("62b6b526e04e1b79cff3b4bb"),
"username" : "GoodGuyGreg",
"title" : "Steals your identity",
"body" : "Raises your credit score"
}
{
"_id" : ObjectId("62b6b526e04e1b79cff3b4bc"),
"username" : "GoodGuyGreg",
"title" : " Reports a bug in your code",
"body" : " Sends you a Pull Request"
}
{
"_id" : ObjectId("62b6b526e04e1b79cff3b4bd"),
"username" : "ScumbagSteve",
"title" : " Borrows something",
"body" : "Sells it"
}
{
"_id" : ObjectId("62b6b526e04e1b79cff3b4be"),
"username" : "ScumbagSteve",
"title" : "Borrows everything",
"body" : "The end"
}
{
"_id" : ObjectId("62b6b526e04e1b79cff3b4bf"),
"username" : "ScumbagSteve",
"title" : "Forks your repo on github",
"body" : "Sets to private"
}
//3.find all posts that was authored by "GoodGuyGreg"
> db.posts.find({ username: 'GoodGuyGreg' }).pretty();
{
"_id" : ObjectId("62b6b526e04e1b79cff3b4ba"),
"username" : "GoodGuyGreg",
"title" : " Passes out at party",
"body" : " Wakes up early and cleans house"
}
{
"_id" : ObjectId("62b6b526e04e1b79cff3b4bb"),
"username" : "GoodGuyGreg",
"title" : "Steals your identity",
"body" : "Raises your credit score"
}
{
"_id" : ObjectId("62b6b526e04e1b79cff3b4bc"),
"username" : "GoodGuyGreg",
"title" : " Reports a bug in your code",
"body" : " Sends you a Pull Request"
}
//4.find all posts that was authored by "ScumbagSteve"
> db.posts.find({ username: 'ScumbagSteve' }).pretty();
{
"_id" : ObjectId("62b6b526e04e1b79cff3b4bd"),
"username" : "ScumbagSteve",
"title" : " Borrows something",
"body" : "Sells it"
}
{
"_id" : ObjectId("62b6b526e04e1b79cff3b4be"),
"username" : "ScumbagSteve",
"title" : "Borrows everything",
"body" : "The end"
}
{
"_id" : ObjectId("62b6b526e04e1b79cff3b4bf"),
"username" : "ScumbagSteve",
"title" : "Forks your repo on github",
"body" : "Sets to private"
}
//5.find all comments
> db.comments.find({}).pretty();
{
"_id" : ObjectId("62b6b82ae04e1b79cff3b4c0"),
"username" : "GoodGuyGreg",
"comment" : "Hope you got a good deal!",
"post" : ObjectId("62b2131f58e2771bd28580f0")
}
{
"_id" : ObjectId("62b6b82ae04e1b79cff3b4c1"),
"username" : "GoodGuyGreg",
"comment" : "Don't violate the licensing agreement!",
"post" : ObjectId("62b2131f58e2771bd28580f1")
}
{
"_id" : ObjectId("62b6b82ae04e1b79cff3b4c2"),
"username" : "GoodGuyGreg",
"comment" : "Don't violate the licensing agreement!",
"post" : ObjectId("62b2131f58e2771bd28580f2")
}
{
"_id" : ObjectId("62b6b82ae04e1b79cff3b4c3"),
"username" : "ScumbagSteve",
"comment" : "It still isn't clean",
"post" : ObjectId("62b2131f58e2771bd28580ed")
}
{
"_id" : ObjectId("62b6b82ae04e1b79cff3b4c4"),
"username" : "ScumbagSteve",
"comment" : "Denied your PR cause I found a hack",
"post" : ObjectId("62b2131f58e2771bd28580ef")
}
//6.find all comments that was authored by "GoodGuyGreg"
> db.comments.find({ username: "GoodGuyGreg" }).pretty();
{
"_id" : ObjectId("62b6b82ae04e1b79cff3b4c0"),
"username" : "GoodGuyGreg",
"comment" : "Hope you got a good deal!",
"post" : ObjectId("62b2131f58e2771bd28580f0")
}
{
"_id" : ObjectId("62b6b82ae04e1b79cff3b4c1"),
"username" : "GoodGuyGreg",
"comment" : "Don't violate the licensing agreement!",
"post" : ObjectId("62b2131f58e2771bd28580f1")
}
{
"_id" : ObjectId("62b6b82ae04e1b79cff3b4c2"),
"username" : "GoodGuyGreg",
"comment" : "Don't violate the licensing agreement!",
"post" : ObjectId("62b2131f58e2771bd28580f2")
}
//7.find all comments that was authored by "ScumbagSteve"
> db.comments.find({ username: "ScumbagSteve" }).pretty();
{
"_id" : ObjectId("62b6b82ae04e1b79cff3b4c3"),
"username" : "ScumbagSteve",
"comment" : "It still isn't clean",
"post" : ObjectId("62b2131f58e2771bd28580ed")
}
{
"_id" : ObjectId("62b6b82ae04e1b79cff3b4c4"),
"username" : "ScumbagSteve",
"comment" : "Denied your PR cause I found a hack",
"post" : ObjectId("62b2131f58e2771bd28580ef")
} |
> db.movies.insertMany([{
... title: "Fight Club", writer: "Chuck Palahniuk", year: 1999, actors: ["Brad Pitt", "EEdward Norton"]},
... {title: "Pulp Fiction", writer: "Quentin Tarantino", year: 1994, actors: ["John Travolta", "Uma Thurman"]},
... {title: "Inglorius Basterds", writer: "Quentin Tarantino", year: 2009, actors: ["Brad Pitt", "Diane Kruger", "Eli Roth"]},
... {title: "The Hobbit: An Unexpected Journey", writer: "J.R.R. Tolkein", year: 2012, franchise: "The Hobbit"},
... {title: "The Hobbit: The Desolation of smug", writer: "J.R.R. Tolkein", year: 2013, franchise: "The Hobbit"},
... {title: "The Hobbit: The Battle of the Five Armies", writer: "J.R.R. Tolkein", year: 2012, franchise: "The Hobbit",
... synopsis: "Bilbo and Company are forced to engage in a war against an array of combatants and keep the the lonely mountains from falling into the hands of a rising darkness."},
... {title: "Pee Wee Herman's Big Adventure"}, {title: "Avatar"}])
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("62a81d49a0464c977fa8f2e2"),
ObjectId("62a81d49a0464c977fa8f2e3"),
ObjectId("62a81d49a0464c977fa8f2e4"),
ObjectId("62a81d49a0464c977fa8f2e5"),
ObjectId("62a81d49a0464c977fa8f2e6"),
ObjectId("62a81d49a0464c977fa8f2e7"),
ObjectId("62a81d49a0464c977fa8f2e8"),
ObjectId("62a81d49a0464c977fa8f2e9")
]
}
> db.movies.find().pretty()
{
"_id" : ObjectId("62a81d49a0464c977fa8f2e2"),
"title" : "Fight Club",
"writer" : "Chuck Palahniuk",
"year" : 1999,
"actors" : [
"Brad Pitt",
"EEdward Norton"
]
}
{
"_id" : ObjectId("62a81d49a0464c977fa8f2e3"),
"title" : "Pulp Fiction",
"writer" : "Quentin Tarantino",
"year" : 1994,
"actors" : [
"John Travolta",
"Uma Thurman"
]
}
{
"_id" : ObjectId("62a81d49a0464c977fa8f2e4"),
"title" : "Inglorius Basterds",
"writer" : "Quentin Tarantino",
"year" : 2009,
"actors" : [
"Brad Pitt",
"Diane Kruger",
"Eli Roth"
]
}
{
"_id" : ObjectId("62a81d49a0464c977fa8f2e5"),
"title" : "The Hobbit: An Unexpected Journey",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit"
}
{
"_id" : ObjectId("62a81d49a0464c977fa8f2e6"),
"title" : "The Hobbit: The Desolation of smug",
"writer" : "J.R.R. Tolkein",
"year" : 2013,
"franchise" : "The Hobbit"
}
{
"_id" : ObjectId("62a81d49a0464c977fa8f2e7"),
"title" : "The Hobbit: The Battle of the Five Armies",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : "Bilbo and Company are forced to engage in a war against an array of combatants and keep the the lonely mountains from falling into the hands of a rising darkness."
}
{
"_id" : ObjectId("62a81d49a0464c977fa8f2e8"),
"title" : "Pee Wee Herman's Big Adventure"
}
{ "_id" : ObjectId("62a81d49a0464c977fa8f2e9"), "title" : "Avatar" }
> db.movies.find({writer:"Quentin Tarantino"}).pretty()
{
"_id" : ObjectId("62a81d49a0464c977fa8f2e3"),
"title" : "Pulp Fiction",
"writer" : "Quentin Tarantino",
"year" : 1994,
"actors" : [
"John Travolta",
"Uma Thurman"
]
}
{
"_id" : ObjectId("62a81d49a0464c977fa8f2e4"),
"title" : "Inglorius Basterds",
"writer" : "Quentin Tarantino",
"year" : 2009,
"actors" : [
"Brad Pitt",
"Diane Kruger",
"Eli Roth"
]
}
> db.movies.find({"actors":{"$in":["Brad Pitt"]}}).pretty()
{
"_id" : ObjectId("62a81d49a0464c977fa8f2e2"),
"title" : "Fight Club",
"writer" : "Chuck Palahniuk",
"year" : 1999,
"actors" : [
"Brad Pitt",
"EEdward Norton"
]
}
{
"_id" : ObjectId("62a81d49a0464c977fa8f2e4"),
"title" : "Inglorius Basterds",
"writer" : "Quentin Tarantino",
"year" : 2009,
"actors" : [
"Brad Pitt",
"Diane Kruger",
"Eli Roth"
]
}
> db.movies.find({"franchise":"The Hobbit"}).pretty()
{
"_id" : ObjectId("62a81d49a0464c977fa8f2e5"),
"title" : "The Hobbit: An Unexpected Journey",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit"
}
{
"_id" : ObjectId("62a81d49a0464c977fa8f2e6"),
"title" : "The Hobbit: The Desolation of smug",
"writer" : "J.R.R. Tolkein",
"year" : 2013,
"franchise" : "The Hobbit"
}
{
"_id" : ObjectId("62a81d49a0464c977fa8f2e7"),
"title" : "The Hobbit: The Battle of the Five Armies",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : "Bilbo and Company are forced to engage in a war against an array of combatants and keep the the lonely mountains from falling into the hands of a rising darkness."
}
> db.movies.find({$and:[{"year":{"$gte":1990}},{"year":{"$lte":1999}}]}).pretty()
{
"_id" : ObjectId("62a81d49a0464c977fa8f2e2"),
"title" : "Fight Club",
"writer" : "Chuck Palahniuk",
"year" : 1999,
"actors" : [
"Brad Pitt",
"EEdward Norton"
]
}
{
"_id" : ObjectId("62a81d49a0464c977fa8f2e3"),
"title" : "Pulp Fiction",
"writer" : "Quentin Tarantino",
"year" : 1994,
"actors" : [
"John Travolta",
"Uma Thurman"
]
}
> db.movies.find({$or:[{"year":{"$gt":2010}},{"year":{"$lt":2000}}]}).pretty()
{
"_id" : ObjectId("62a81d49a0464c977fa8f2e2"),
"title" : "Fight Club",
"writer" : "Chuck Palahniuk",
"year" : 1999,
"actors" : [
"Brad Pitt",
"EEdward Norton"
]
}
{
"_id" : ObjectId("62a81d49a0464c977fa8f2e3"),
"title" : "Pulp Fiction",
"writer" : "Quentin Tarantino",
"year" : 1994,
"actors" : [
"John Travolta",
"Uma Thurman"
]
}
{
"_id" : ObjectId("62a81d49a0464c977fa8f2e5"),
"title" : "The Hobbit: An Unexpected Journey",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit"
}
{
"_id" : ObjectId("62a81d49a0464c977fa8f2e6"),
"title" : "The Hobbit: The Desolation of smug",
"writer" : "J.R.R. Tolkein",
"year" : 2013,
"franchise" : "The Hobbit"
}
{
"_id" : ObjectId("62a81d49a0464c977fa8f2e7"),
"title" : "The Hobbit: The Battle of the Five Armies",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : "Bilbo and Company are forced to engage in a war against an array of combatants and keep the the lonely mountains from falling into the hands of a rising darkness."
}
> db.movies.updateOne({"title":"The Hobbit: An Unexpected Journey"},{$set:{"synopsis":"A reluctant hobbit, Bilbo Baggins, sets out to the Lonely Mountain with a spirited group of dwarves to reclaim their mountain home - and the gold within it - from the dragon Smaug."}})
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
> db.movies.updateOne({"title":"The Hobbit: The Desolation of Smaug"},{$set:{"synopsis":"The dwarves, along with Bilbo Baggins and Gandalf the Grey, continue their quest to reclaim Erebor, their homeland, from Smaug. Bilbo Baggins is in possession of a mysterious and magical ring."}})
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
> db.movies.updateOne({"title":"Pulp Fiction"},{$push:{"actors":"Samuel L. Jackson"}})
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
> db.movies.find({"title":"Pulp Fiction"},{_id:0,title:1,actors:1})
{ "title" : "Pulp Fiction", "actors" : [ "John Travolta", "Uma Thurman", "Samuel L. Jackson" ] }
> db.movies.find({"synopsis":{"$regex":"Bilbo"}}).pretty()
{
"_id" : ObjectId("62a81d49a0464c977fa8f2e5"),
"title" : "The Hobbit: An Unexpected Journey",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : "A reluctant hobbit, Bilbo Baggins, sets out to the Lonely Mountain with a spirited group of dwarves to reclaim their mountain home - and the gold within it - from the dragon Smaug."
}
{
"_id" : ObjectId("62a81d49a0464c977fa8f2e6"),
"title" : "The Hobbit: The Desolation of Smaug",
"writer" : "J.R.R. Tolkein",
"year" : 2013,
"franchise" : "The Hobbit",
"synopsis" : "The dwarves, along with Bilbo Baggins and Gandalf the Grey, continue their quest to reclaim Erebor, their homeland, from Smaug. Bilbo Baggins is in possession of a mysterious and magical ring."
}
{
"_id" : ObjectId("62a81d49a0464c977fa8f2e7"),
"title" : "The Hobbit: The Battle of the Five Armies",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : "Bilbo and Company are forced to engage in a war against an array of combatants and keep the the lonely mountains from falling into the hands of a rising darkness."
}
> db.movies.find({"synopsis":{"$regex":"Gandalf"}}).pretty()
{
"_id" : ObjectId("62a81d49a0464c977fa8f2e6"),
"title" : "The Hobbit: The Desolation of Smaug",
"writer" : "J.R.R. Tolkein",
"year" : 2013,
"franchise" : "The Hobbit",
"synopsis" : "The dwarves, along with Bilbo Baggins and Gandalf the Grey, continue their quest to reclaim Erebor, their homeland, from Smaug. Bilbo Baggins is in possession of a mysterious and magical ring."
}
> db.movies.find({"synopsis":{"$regex":"Bilbo",$not:{"$regex":"Gandalf"}}}).pretty()
{
"_id" : ObjectId("62a81d49a0464c977fa8f2e5"),
"title" : "The Hobbit: An Unexpected Journey",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : "A reluctant hobbit, Bilbo Baggins, sets out to the Lonely Mountain with a spirited group of dwarves to reclaim their mountain home - and the gold within it - from the dragon Smaug."
}
{
"_id" : ObjectId("62a81d49a0464c977fa8f2e7"),
"title" : "The Hobbit: The Battle of the Five Armies",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : "Bilbo and Company are forced to engage in a war against an array of combatants and keep the the lonely mountains from falling into the hands of a rising darkness."
}
> db.movies.find({"$or":[{"synopsis":{"$regex":"dwarves"}},{"synopsis":{"$regex":"hobbit"}}]}).pretty()
{
"_id" : ObjectId("62a81d49a0464c977fa8f2e5"),
"title" : "The Hobbit: An Unexpected Journey",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : "A reluctant hobbit, Bilbo Baggins, sets out to the Lonely Mountain with a spirited group of dwarves to reclaim their mountain home - and the gold within it - from the dragon Smaug."
}
{
"_id" : ObjectId("62a81d49a0464c977fa8f2e6"),
"title" : "The Hobbit: The Desolation of Smaug",
"writer" : "J.R.R. Tolkein",
"year" : 2013,
"franchise" : "The Hobbit",
"synopsis" : "The dwarves, along with Bilbo Baggins and Gandalf the Grey, continue their quest to reclaim Erebor, their homeland, from Smaug. Bilbo Baggins is in possession of a mysterious and magical ring."
}
> db.movies.find({"$and":[{"synopsis":{"$regex":"gold"}},{"synopsis":{"$regex":"dragon"}}]}).pretty()
{
"_id" : ObjectId("62a81d49a0464c977fa8f2e5"),
"title" : "The Hobbit: An Unexpected Journey",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : "A reluctant hobbit, Bilbo Baggins, sets out to the Lonely Mountain with a spirited group of dwarves to reclaim their mountain home - and the gold within it - from the dragon Smaug."
}
> db.movies.deleteOne({"title":"Pee Wee Herman's Big Adventure"})
{ "acknowledged" : true, "deletedCount" : 1 }
> db.movies.find({},{_id:0,title:1}).pretty()
{ "title" : "Fight Club" }
{ "title" : "Pulp Fiction" }
{ "title" : "Inglorius Basterds" }
{ "title" : "The Hobbit: An Unexpected Journey" }
{ "title" : "The Hobbit: The Desolation of Smaug" }
{ "title" : "The Hobbit: The Battle of the Five Armies" }
{ "title" : "Avatar" }
> db.movies.deleteOne({"title":"Avatar"})
{ "acknowledged" : true, "deletedCount" : 1 }
> db.movies.find({},{_id:0,title:1}).pretty()
{ "title" : "Fight Club" }
{ "title" : "Pulp Fiction" }
{ "title" : "Inglorius Basterds" }
{ "title" : "The Hobbit: An Unexpected Journey" }
{ "title" : "The Hobbit: The Desolation of Smaug" }
{ "title" : "The Hobbit: The Battle of the Five Armies" }
> db.users.insertMany([{"username":"GoodGuyGreg","first_name":"Good Guy", "last_name" : "Greg"},{ "username" : "ScumbagSteve", "full_name" : {"first" : "Scumbag" , "last" : "Steve"}}])
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("62aed6f72965e0d6d04e8020"),
ObjectId("62aed6f72965e0d6d04e8021")
]
}
> db.users.find().pretty()
{
"_id" : ObjectId("62aed6f72965e0d6d04e8020"),
"username" : "GoodGuyGreg",
"first_name" : "Good Guy",
"last_name" : "Greg"
}
{
"_id" : ObjectId("62aed6f72965e0d6d04e8021"),
"username" : "ScumbagSteve",
"full_name" : {
"first" : "Scumbag",
"last" : "Steve"
}
}
> db.posts.insertMany([{"username":"GoodGuyGreg","title" :" Passes out at party", "body" :" Wakes up early and cleans house"},{"username":"GoodGuyGreg","title" :"Steals your identity", "body" :"Raises your credit score"},{"username":"GoodGuyGreg","title" :" Reports a bug in your code", "body" :" Sends you a Pull Request"},{"username":"ScumbagSteve","title" :" Borrows something", "body" :"Sells it"},{"username":"ScumbagSteve","title" :"Borrows everything", "body" :"The end"},{"username":"ScumbagSteve","title" :"Forks your repo on github", "body" :"Sets to private"}])
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("62aedb802965e0d6d04e8022"),
ObjectId("62aedb802965e0d6d04e8023"),
ObjectId("62aedb802965e0d6d04e8024"),
ObjectId("62aedb802965e0d6d04e8025"),
ObjectId("62aedb802965e0d6d04e8026"),
ObjectId("62aedb802965e0d6d04e8027")
]
}
> db.users.find().pretty()
{
"_id" : ObjectId("62aed6f72965e0d6d04e8020"),
"username" : "GoodGuyGreg",
"first_name" : "Good Guy",
"last_name" : "Greg"
}
{
"_id" : ObjectId("62aed6f72965e0d6d04e8021"),
"username" : "ScumbagSteve",
"full_name" : {
"first" : "Scumbag",
"last" : "Steve"
}
}
> db.posts.find().pretty()
{
"_id" : ObjectId("62aedb802965e0d6d04e8022"),
"username" : "GoodGuyGreg",
"title" : " Passes out at party",
"body" : " Wakes up early and cleans house"
}
{
"_id" : ObjectId("62aedb802965e0d6d04e8023"),
"username" : "GoodGuyGreg",
"title" : "Steals your identity",
"body" : "Raises your credit score"
}
{
"_id" : ObjectId("62aedb802965e0d6d04e8024"),
"username" : "GoodGuyGreg",
"title" : " Reports a bug in your code",
"body" : " Sends you a Pull Request"
}
{
"_id" : ObjectId("62aedb802965e0d6d04e8025"),
"username" : "ScumbagSteve",
"title" : " Borrows something",
"body" : "Sells it"
}
{
"_id" : ObjectId("62aedb802965e0d6d04e8026"),
"username" : "ScumbagSteve",
"title" : "Borrows everything",
"body" : "The end"
}
{
"_id" : ObjectId("62aedb802965e0d6d04e8027"),
"username" : "ScumbagSteve",
"title" : "Forks your repo on github",
"body" : "Sets to private"
}
> db.posts.find({username:"GoodGuyGreg"}).pretty()
{
"_id" : ObjectId("62aedb802965e0d6d04e8022"),
"username" : "GoodGuyGreg",
"title" : " Passes out at party",
"body" : " Wakes up early and cleans house"
}
{
"_id" : ObjectId("62aedb802965e0d6d04e8023"),
"username" : "GoodGuyGreg",
"title" : "Steals your identity",
"body" : "Raises your credit score"
}
{
"_id" : ObjectId("62aedb802965e0d6d04e8024"),
"username" : "GoodGuyGreg",
"title" : " Reports a bug in your code",
"body" : " Sends you a Pull Request"
}
> db.posts.find({username:"ScumbagSteve"}).pretty()
{
"_id" : ObjectId("62aedb802965e0d6d04e8025"),
"username" : "ScumbagSteve",
"title" : " Borrows something",
"body" : "Sells it"
}
{
"_id" : ObjectId("62aedb802965e0d6d04e8026"),
"username" : "ScumbagSteve",
"title" : "Borrows everything",
"body" : "The end"
}
{
"_id" : ObjectId("62aedb802965e0d6d04e8027"),
"username" : "ScumbagSteve",
"title" : "Forks your repo on github",
"body" : "Sets to private"
} |
// Show Databases Output => use mongo_practice Output => { ok: 1 } //=====================================================================// // 2. Insert Documents db.movies.insert({ title : "Fight Club", writer : "Chuck Palahniuk", year : 1999,actors : ["Brad Pitt","Edward Norton"]}); Output => //------------------------------------------------ Output => //------------------------------------------------------------------- Output => //=====================================================================================// // 1. get all documents db.movies.find().pretty(); Output => [ // 2. get all documents with writer set to "Quentin Tarantino" db.movies.find({writer: "Quentin Tarantino"}); Output => //-------------------------------------------------------------- db.movies.find({actors: "Brad Pitt"}); Output => //----------------------------------------------------------------- db.movies.find({franchise : "The Hobbit"}); Output => [ //------------------------------------------------------------------------- db.movies.find({"year": {$gte: 1900, $lte: 1999}}) Output => [ //----------------------------------------------------------------------------- db.movies.find({$or: [{"year": {$lte : 2000}}, {"year" : {$gte: 2010}}] }) Output => [ //===========================================================================================// // 1. add a synopsis to "The Hobbit: An Unexpected Journey" : "A reluctant hobbit, Bilbo Baggins, sets out to the Lonely Mountain with a spirited group of dwarves to reclaim their mountain home - and the gold within it - from the dragon Smaug." db.movies.updateOne({'title': "The Hobbit: An Unexpected Journey"}, {$set : {'synopsis': "A reluctant hobbit, Bilbo Baggins, sets out to the Lonely Mountain with a spirited group of dwarves to reclaim their mountain home - and the gold within it - from the dragon Smaug."}}) Output => { //----------------------------------------------------------------------------- db.movies.updateOne({'title': "The Hobbit: The Desolation of Smaug"}, {$set : {'synopsis': "The dwarves, along with Bilbo Baggins and Gandalf the Grey, continue their quest to reclaim Erebor, their homeland, from Smaug. Bilbo Baggins is in possession of a mysterious and magical ring."}}) Output => { //------------------------------------------------------------------------------ db.movies.updateOne({'title': "Pulp Fiction"}, {$set : {'actor': "Samuel L. Jackson"}}) Output => // ========================================================================================// // 1. find all movies that have a synopsis that contains the word "Bilbo" db.movies.find({synopsis : {"$regex": "Bilbo"}}) Output => [ //------------------------------------------------------------------------ db.movies.find({synopsis : {"$regex": "Gandalf"}}) Output => db.movies.find({synopsis : {"$regex": "Bilbo", $not : {"$regex": "Gandalf"}}}) Output => //--------------------------------------------------------------------------- db.movies.find({$and: [{synopsis: {'$regex': "gold"}}, {synopsis: {'$regex': "dragon"}}]}) Output => [ //----------------------------------------------------------------------------- db.movies.find({$and : [{synopsis: {'$regex': "gold" }}, {synopsis : {'$regex': "dragon"}}]}); // 1. delete the movie "Pee Wee Herman's Big Adventure" db.movies.remove({title: "Pee Wee Herman's Big Adventure"}); Output => //------------------------------------------------------------------------------ db.movies.remove({title: "Avatar"}); Output => //================================================================================ db.createCollection("users"); //----------------------------------------------------------------- db.users.insertMany([ Output => //------------------------------------------------------------------- db.posts.insertMany([ Output => //==============================================================================// db.comments.insertMany([ Output => //====================================================================================// // 1. find all users db.users.find().pretty(); Output => [ db.posts.find().pretty(); Output => [ //---------------------------------------------------------------- db.posts.find({username : "GoodGuyGreg"}).pretty() Output => //--------------------------------------------------------------------- db.posts.find({username : "ScumbagSteve"}).pretty() Output => [ db.comments.find().pretty(); Output => [ db.comments.find({username: "GoodGuyGreg"}) Output => db.comments.find({username: "ScumbagSteve"}) Output => [ // -------------------------------------------------------------- db.comments.find({post: ObjectId("6495e6d15ea515c3fe0d4206")}) Output => [ |
show dbs Output => admin 40.00 KiB use mongo_practice Output => { ok: 1 } /********* Insert Documents ******************/ //2.Insert Documents db.movies.insert({ title : "Fight Club", writer : "Chuck Palahniuk", year : 1999,actors : ["Brad Pitt","Edward Norton"]}); Output => //---------------- Output => //------------------------------------------------------------------- Output => //=====================================================================================// // 1. get all documents db.movies.find().pretty(); Output => [ // 2. get all documents with writer set to "Quentin Tarantino" db.movies.find({writer: "Quentin Tarantino"}); Output => //-------------------------------------------------------------- db.movies.find({actors: "Brad Pitt"}); Output => //----------------------------------------------------------------- db.movies.find({franchise : "The Hobbit"}); Output => [ //------------------------------------------------------------------------- db.movies.find({"year": {$gte: 1900, $lte: 1999}}) Output => [ //----------------------------------------------------------------------------- db.movies.find({$or: [{"year": {$lte : 2000}}, {"year" : {$gte: 2010}}] }) Output => [ //===========================================================================================// // 1. add a synopsis to "The Hobbit: An Unexpected Journey" : "A reluctant hobbit, Bilbo Baggins, sets out to the Lonely Mountain with a spirited group of dwarves to reclaim their mountain home - and the gold within it - from the dragon Smaug." db.movies.updateOne({'title': "The Hobbit: An Unexpected Journey"}, {$set : {'synopsis': "A reluctant hobbit, Bilbo Baggins, sets out to the Lonely Mountain with a spirited group of dwarves to reclaim their mountain home - and the gold within it - from the dragon Smaug."}}) Output => { //----------------------------------------------------------------------------- db.movies.updateOne({'title': "The Hobbit: The Desolation of Smaug"}, {$set : {'synopsis': "The dwarves, along with Bilbo Baggins and Gandalf the Grey, continue their quest to reclaim Erebor, their homeland, from Smaug. Bilbo Baggins is in possession of a mysterious and magical ring."}}) Output => { //------------------------------------------------------------------------------ db.movies.updateOne({'title': "Pulp Fiction"}, {$set : {'actor': "Samuel L. Jackson"}}) Output => // ========================================================================================// // 1. find all movies that have a synopsis that contains the word "Bilbo" db.movies.find({synopsis : {"$regex": "Bilbo"}}) Output => [ //------------------------------------------------------------------------ db.movies.find({synopsis : {"$regex": "Gandalf"}}) Output => db.movies.find({synopsis : {"$regex": "Bilbo", $not : {"$regex": "Gandalf"}}}) Output => //--------------------------------------------------------------------------- db.movies.find({$and: [{synopsis: {'$regex': "gold"}}, {synopsis: {'$regex': "dragon"}}]}) Output => [ //----------------------------------------------------------------------------- db.movies.find({$and : [{synopsis: {'$regex': "gold" }}, {synopsis : {'$regex': "dragon"}}]}); // 1. delete the movie "Pee Wee Herman's Big Adventure" db.movies.remove({title: "Pee Wee Herman's Big Adventure"}); Output => //------------------------------------------------------------------------------ db.movies.remove({title: "Avatar"}); Output => //================================================================================ db.createCollection("users"); //----------------------------------------------------------------- db.users.insertMany([ Output => //------------------------------------------------------------------- db.posts.insertMany([ Output => //==============================================================================// db.comments.insertMany([ Output => /*********** Querying related collections *************/ //1.find all users db.users.find().pretty(); Output => [ db.posts.find().pretty(); Output => [ //---------------------------------------------------------------- db.posts.find({username : "GoodGuyGreg"}).pretty() Output => //--------------------------------------------------------------------- db.posts.find({username : "ScumbagSteve"}).pretty() Output => [ db.comments.find().pretty(); Output => [ db.comments.find({username: "GoodGuyGreg"}) Output => db.comments.find({username: "ScumbagSteve"}) Output => [ // -------------------------------------------------------------- db.comments.find({post: ObjectId("6495e6d15ea515c3fe0d4206")}) Output => [ |
Mongo Db commands with comments// MongoDB shell version v5.0.16
// connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
// Implicit session: session { "id" : UUID("1151cea4-beb4-43e0-905e-38a762485c25") }
// MongoDB server version: 5.0.16
// ================
// Warning: the "mongo" shell has been superseded by "mongosh",
// which delivers improved usability and compatibility.The "mongo" shell has been deprecated and will be removed in
// an upcoming release.
// For installation instructions, see
// https://docs.mongodb.com/mongodb-shell/install/
// ================
// ---
// The server generated these startup warnings when booting:
// 2023-06-19T11:23:18.130+05:30: Access control is not enabled for the database. Read and write access to data and configuration
// Connect to a running mongo instance, use a database named mongo_practice.
// Document all your queries in a javascript file to use as a reference.
// Insert Documents
// Insert the following documents into a movies collection.
> db.movies.insertMany([{title: "Fight Club", writer: "Chuck Palahniuk", year: 1999, actors: ["Brad Pitt", "EEdward Norton"]},
... {title: "Pulp Fiction", writer: "Quentin Tarantino", year: 1994, actors: ["John Travolta", "Uma Thurman"]},
... {title: "Inglorius Basterds", writer: "Quentin Tarantino", year: 2009, actors: ["Brad Pitt", "Diane Kruger", "Eli Roth"]},
... {title: "The Hobbit: An Unexpected Journey", writer: "J.R.R. Tolkein", year: 2012, franchise: "The Hobbit"},
... {title: "The Hobbit: The Desolation of smug", writer: "J.R.R. Tolkein", year: 2013, franchise: "The Hobbit"},
... synopsis: "Bilbo and Company are forced to engage in a war against an array of combatants and keep the the lonely mountains from falling into the hands of a rising darkness."}, ngage in a war against an array of combatants and keep the the lonely mountains from
... {title: "Pee Wee Herman's Big Adventure"}, {title: "Avatar"}]);
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("6496d205418592235b6e658c"),
ObjectId("6496d205418592235b6e658d"),
ObjectId("6496d205418592235b6e658e"),
ObjectId("6496d205418592235b6e658f"),
ObjectId("6496d205418592235b6e6590"),
ObjectId("6496d205418592235b6e6591"),
ObjectId("6496d205418592235b6e6592"),
ObjectId("6496d205418592235b6e6593")
]
}
// Query / Find Documents
// query the movies collection to
// get all documents
// get all documents with writer set to "Quentin Tarantino"
// get all documents where actors include "Brad Pitt"
// get all documents with franchise set to "The Hobbit"
// get all movies released in the 90s
// get all movies released before the year 2000 or after 2010
> db.movies.find({}).pretty();
{
"_id" : ObjectId("6496d205418592235b6e658c"),
"title" : "Fight Club",
"writer" : "Chuck Palahniuk",
"year" : 1999,
"actors" : [
"Brad Pitt",
"EEdward Norton"
]
}
{
"_id" : ObjectId("6496d205418592235b6e658d"),
"title" : "Pulp Fiction",
"writer" : "Quentin Tarantino",
"year" : 1994,
"actors" : [
"John Travolta",
"Uma Thurman"
]
}
{
"_id" : ObjectId("6496d205418592235b6e658e"),
"title" : "Inglorius Basterds",
"writer" : "Quentin Tarantino",
"year" : 2009,
"actors" : [
"Brad Pitt",
"Diane Kruger",
"Eli Roth"
]
}
{
"_id" : ObjectId("6496d205418592235b6e658f"),
"title" : "The Hobbit: An Unexpected Journey",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit"
}
{
"_id" : ObjectId("6496d205418592235b6e6590"),
"title" : "The Hobbit: The Desolation of smug",
"writer" : "J.R.R. Tolkein",
"year" : 2013,
"franchise" : "The Hobbit"
}
{
"_id" : ObjectId("6496d205418592235b6e6591"),
"title" : "The Hobbit: The Battle of the Five Armies",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : "Bilbo and Company are forced to engage in a war against an array of combatants and keep the the lonely mountains from falling into the hands of a rising darkness."
}
{
"_id" : ObjectId("6496d205418592235b6e6592"),
"title" : "Pee Wee Herman's Big Adventure"
}
{ "_id" : ObjectId("6496d205418592235b6e6593"), "title" : "Avatar" }
> db.movies.find().pretty();
{
"_id" : ObjectId("6496d205418592235b6e658c"),
"title" : "Fight Club",
"writer" : "Chuck Palahniuk",
"year" : 1999,
"actors" : [
"Brad Pitt",
"EEdward Norton"
]
}
{
"_id" : ObjectId("6496d205418592235b6e658d"),
"title" : "Pulp Fiction",
"writer" : "Quentin Tarantino",
"year" : 1994,
"actors" : [
"John Travolta",
"Uma Thurman"
]
}
{
"_id" : ObjectId("6496d205418592235b6e658e"),
"title" : "Inglorius Basterds",
"writer" : "Quentin Tarantino",
"year" : 2009,
"actors" : [
"Brad Pitt",
"Diane Kruger",
"Eli Roth"
]
}
{
"_id" : ObjectId("6496d205418592235b6e658f"),
"title" : "The Hobbit: An Unexpected Journey",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit"
}
{
"_id" : ObjectId("6496d205418592235b6e6590"),
"title" : "The Hobbit: The Desolation of smug",
"writer" : "J.R.R. Tolkein",
"year" : 2013,
"franchise" : "The Hobbit"
}
{
"_id" : ObjectId("6496d205418592235b6e6591"),
"title" : "The Hobbit: The Battle of the Five Armies",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : "Bilbo and Company are forced to engage in a war against an array of combatants and keep the the lonely mountains from falling into the hands of a rising darkness."
}
{
"_id" : ObjectId("6496d205418592235b6e6592"),
"title" : "Pee Wee Herman's Big Adventure"
}
{ "_id" : ObjectId("6496d205418592235b6e6593"), "title" : "Avatar" }
> db.movies.find({ $and : [ {year : { $gte :1990 }}, { year :{ $lte : 1999 }} ] }).pretty();
{
"_id" : ObjectId("6496d205418592235b6e658c"),
"title" : "Fight Club",
"writer" : "Chuck Palahniuk",
"year" : 1999,
"actors" : [
"Brad Pitt",
"EEdward Norton"
]
}
{
"_id" : ObjectId("6496d205418592235b6e658d"),
"title" : "Pulp Fiction",
"writer" : "Quentin Tarantino",
"year" : 1994,
"actors" : [
"John Travolta",
"Uma Thurman"
]
}
> db.movies.find({ $or : [ {year : { $gt :2010 }}, { year :{ $lt : 2000 }} ] }).pretty();
{
"_id" : ObjectId("6496d205418592235b6e658c"),
"title" : "Fight Club",
"writer" : "Chuck Palahniuk",
"year" : 1999,
"actors" : [
"Brad Pitt",
"EEdward Norton"
]
}
{
"_id" : ObjectId("6496d205418592235b6e658d"),
"title" : "Pulp Fiction",
"writer" : "Quentin Tarantino",
"year" : 1994,
"actors" : [
"John Travolta",
"Uma Thurman"
]
}
{
"_id" : ObjectId("6496d205418592235b6e658f"),
"title" : "The Hobbit: An Unexpected Journey",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit"
}
{
"_id" : ObjectId("6496d205418592235b6e6590"),
"title" : "The Hobbit: The Desolation of smug",
"writer" : "J.R.R. Tolkein",
"year" : 2013,
"franchise" : "The Hobbit"
}
{
"_id" : ObjectId("6496d205418592235b6e6591"),
"title" : "The Hobbit: The Battle of the Five Armies",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : "Bilbo and Company are forced to engage in a war against an array of combatants and keep the the lonely mountains db.movies.updateOne({"title":"The Hobbit: An Unexpected Journey"},{$set:{"synopsis":"A reluctant hobbit, Bilbo Baggins, sets out to the Lonely Mountain with a spirited group of dwarves to reclaim their mountain home - and the gold within it - from the dragon Smaug."}});onely Mountain with a spirited group of dwarves to reclaim their mountain home - and the gold within it - from the dragon Smaug
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
> db.movies.find().pretty();
{
"_id" : ObjectId("6496d205418592235b6e658c"),
"title" : "Fight Club",
"writer" : "Chuck Palahniuk",
"year" : 1999,
"actors" : [
"Brad Pitt",
"EEdward Norton"
]
}
{
"_id" : ObjectId("6496d205418592235b6e658d"),
"title" : "Pulp Fiction",
"writer" : "Quentin Tarantino",
"year" : 1994,
"actors" : [
"John Travolta",
"Uma Thurman"
]
}
{
"_id" : ObjectId("6496d205418592235b6e658e"),
"title" : "Inglorius Basterds",
"writer" : "Quentin Tarantino",
"year" : 2009,
"actors" : [
"Brad Pitt",
"Diane Kruger",
"Eli Roth"
]
}
{
"_id" : ObjectId("6496d205418592235b6e658f"),
"title" : "The Hobbit: An Unexpected Journey",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : "A reluctant hobbit, Bilbo Baggins, sets out to the Lonely Mountain with a spirited group of dwarves to reclaim their mountain home - and the gold within it - from the dragon Smaug."
}
{
"_id" : ObjectId("6496d205418592235b6e6590"),
"title" : "The Hobbit: The Desolation of smug",
"writer" : "J.R.R. Tolkein",
"year" : 2013,
"franchise" : "The Hobbit"
}
{
"_id" : ObjectId("6496d205418592235b6e6591"),
"title" : "The Hobbit: The Battle of the Five Armies",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : "Bilbo and Company are forced to engage in a war against an array of combatants and keep the the lonely mountains from falling into the hands of a rising darkness."
}
{
"_id" : ObjectId("6496d205418592235b6e6592"),
"title" : "Pee Wee Herman's Big Adventure"
}
// Update Documents
// add a synopsis to "The Hobbit: An Unexpected Journey" : "A reluctant hobbit, Bilbo Baggins, sets out to the Lonely Mountain with a spirited group of dwarves to reclaim their mountain home - and the gold within it - from the dragon Smaug."
// add a synopsis to "The Hobbit: The Desolation of Smaug" : "The dwarves, along with Bilbo Baggins and Gandalf the Grey, continue their quest to reclaim Erebor, their homeland, from Smaug. Bilbo Baggins is in possession of a mysterious and magical ring."
// add an actor named "Samuel L. Jackson" to the movie "Pulp Fiction"
db.movies.updateOne({"title":"The Hobbit: The Desolation of smug"},{$set:{"synopsis":"The dwarves, along with Bilbo Baggins and Gandalf the Grey, continue their quest to reclaim Erebor, their homeland, from Smaug. Bilbo Baggins is in possession of a mysterious and magical ring."}});inue their quest to reclaim Erebor, their homeland, from Smaug. Bilbo Baggins is in possession of a mysterious and
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
> db.movies.find().pretty();
{
"_id" : ObjectId("6496d205418592235b6e658c"),
"title" : "Fight Club",
"writer" : "Chuck Palahniuk",
"year" : 1999,
"actors" : [
"Brad Pitt",
"EEdward Norton"
]
}
{
"_id" : ObjectId("6496d205418592235b6e658d"),
"title" : "Pulp Fiction",
"writer" : "Quentin Tarantino",
"year" : 1994,
"actors" : [
"John Travolta",
"Uma Thurman"
]
}
{
"_id" : ObjectId("6496d205418592235b6e658e"),
"title" : "Inglorius Basterds",
"writer" : "Quentin Tarantino",
"year" : 2009,
"actors" : [
"Brad Pitt",
"Diane Kruger",
"Eli Roth"
]
}
{
"_id" : ObjectId("6496d205418592235b6e658f"),
"title" : "The Hobbit: An Unexpected Journey",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : "A reluctant hobbit, Bilbo Baggins, sets out to the Lonely Mountain with a spirited group of dwarves to reclaim their mountain home - and the gold within it - from the dragon Smaug."
}
{
"_id" : ObjectId("6496d205418592235b6e6590"),
"title" : "The Hobbit: The Desolation of smug",
"writer" : "J.R.R. Tolkein",
"year" : 2013,
"franchise" : "The Hobbit",
"synopsis" : "The dwarves, along with Bilbo Baggins and Gandalf the Grey, continue their quest to reclaim Erebor, their homeland, from Smaug. Bilbo Baggins is in possession of a mysterious and magical ring."
}
{
"_id" : ObjectId("6496d205418592235b6e6591"),
"title" : "The Hobbit: The Battle of the Five Armies",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : "Bilbo and Company are forced to engage in a war against an array of combatants and keep the the lonely mountains from falling into the hands of a rising darkness."
}
{
"_id" : ObjectId("6496d205418592235b6e6592"),
"title" : "Pee Wee Herman's Big Adventure"
}
{ "_id" : ObjectId("6496d205418592235b6e6593"), "title" : "Avatar" }
> db.movies.updateOne({"title":"Pulp Fiction"},{$push:{"actors":"Samuel L. Jackson"}});
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
// Text Search
// find all movies that have a synopsis that contains the word "Bilbo"
// find all movies that have a synopsis that contains the word "Gandalf"
// find all movies that have a synopsis that contains the word "Bilbo" and not the word "Gandalf"
// find all movies that have a synopsis that contains the word "dwarves" or "hobbit"
// find all movies that have a synopsis that contains the word "gold" and "dragon"
> db.movies.find({"synopsis":{"$regex":"Bilbo"}}).pretty();
{
"_id" : ObjectId("6496d205418592235b6e658f"),
"title" : "The Hobbit: An Unexpected Journey",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : "A reluctant hobbit, Bilbo Baggins, sets out to the Lonely Mountain with a spirited group of dwarves to reclaim their mountain home - and the gold within it - from the dragon Smaug."
}
{
"_id" : ObjectId("6496d205418592235b6e6590"),
"title" : "The Hobbit: The Desolation of smug",
"writer" : "J.R.R. Tolkein",
"year" : 2013,
"franchise" : "The Hobbit",
"synopsis" : "The dwarves, along with Bilbo Baggins and Gandalf the Grey, continue their quest to reclaim Erebor, their homeland, from Smaug. Bilbo Baggins is in possession of a mysterious and magical ring."
}
{
"_id" : ObjectId("6496d205418592235b6e6591"),
"title" : "The Hobbit: The Battle of the Five Armies",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : "Bilbo and Company are forced to engage in a war against an array of combatants and keep the the lonely mountains from falling into the hands of a rising darkness."
}
> db.movies.find({"synopsis":{"$regex":"Gandalf"}}).pretty();
{
"_id" : ObjectId("6496d205418592235b6e6590"),
"title" : "The Hobbit: The Desolation of smug",
"writer" : "J.R.R. Tolkein",
"year" : 2013,
"franchise" : "The Hobbit",
"synopsis" : "The dwarves, along with Bilbo Baggins and Gandalf the Grey, continue their quest to reclaim Erebor, their homeland, from Smaug. Bilbo Baggins is in possession of a mysterious and magical ring."
}
> db.movies.find({"synopsis":{"$regex":"Gandalf"}},_id=0).pretty();
{
"_id" : ObjectId("6496d205418592235b6e6590"),
"title" : "The Hobbit: The Desolation of smug",
"writer" : "J.R.R. Tolkein",
"year" : 2013,
"franchise" : "The Hobbit",
"synopsis" : "The dwarves, along with Bilbo Baggins and Gandalf the Grey, continue their quest to reclaim Erebor, their homeland, from Smaug. Bilbo Baggins is in possession of a mysterious and magical ring."
}
> db.movies.find({"synopsis":{"$regex":"Gandalf"}}).pretty();
{
"_id" : ObjectId("6496d205418592235b6e6590"),
"title" : "The Hobbit: The Desolation of smug",
"writer" : "J.R.R. Tolkein",
"year" : 2013,
"franchise" : "The Hobbit",
"synopsis" : "The dwarves, along with Bilbo Baggins and Gandalf the Grey, continue their quest to reclaim Erebor, their homeland, from Smaug. Bilbo Baggins is in possession of a mysterious and magical ring."
}
> db.movies.find({"synopsis":{"$regex":"Bilbo",$not:{"$regex":"Gandalf"}}}).pretty();
{
"_id" : ObjectId("6496d205418592235b6e658f"),
"title" : "The Hobbit: An Unexpected Journey",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : "A reluctant hobbit, Bilbo Baggins, sets out to the Lonely Mountain with a spirited group of dwarves to reclaim their mountain home - and the gold within it - from the dragon Smaug."
}
{
"_id" : ObjectId("6496d205418592235b6e6591"),
"title" : "The Hobbit: The Battle of the Five Armies",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : "Bilbo and Company are forced to engage in a war against an array of combatants and keep the the lonely mountains from falling into the hands of a rising darkness."
}
> db.movies.find({"$or":[{"synopsis":{"$regex":"dwarves"}},{"synopsis":{"$regex":"hobbit"}}]}).pretty();
{
"_id" : ObjectId("6496d205418592235b6e658f"),
"title" : "The Hobbit: An Unexpected Journey",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : "A reluctant hobbit, Bilbo Baggins, sets out to the Lonely Mountain with a spirited group of dwarves to reclaim their mountain home - and the gold within it - from the dragon Smaug."
}
{
"_id" : ObjectId("6496d205418592235b6e6590"),
"title" : "The Hobbit: The Desolation of smug",
"writer" : "J.R.R. Tolkein",
"year" : 2013,
"franchise" : "The Hobbit",
"synopsis" : "The dwarves, along with Bilbo Baggins and Gandalf the Grey, continue their quest to reclaim Erebor, their homeland, from Smaug. Bilbo Baggins is in possession of a mysterious and magical ring."
}
> db.movies.find({"$and":[{"synopsis":{"$regex":"gold"}},{"synopsis":{"$regex":"dragon"}}]}).pretty();
{
"_id" : ObjectId("6496d205418592235b6e658f"),
"title" : "The Hobbit: An Unexpected Journey",
"writer" : "J.R.R. Tolkein",
"year" : 2012,
"franchise" : "The Hobbit",
"synopsis" : "A reluctant hobbit, Bilbo Baggins, sets out to the Lonely Mountain with a spirited group of dwarves to reclaim their mountain home - and the gold within it - from the dragon Smaug."
}
// Delete Documents
// delete the movie "Pee Wee Herman's Big Adventure"
// delete the movie "Avatar"
> db.movies.deleteOne({"title":"Pee Wee Herman's Big Adventure"});
{ "acknowledged" : true, "deletedCount" : 1 }
> db.movies.deleteOne({"title":"Avatar"});
{ "acknowledged" : true, "deletedCount" : 1 }
> db.movies.find({},{_id:0,title:1}).pretty();
{ "title" : "Fight Club" }
{ "title" : "Pulp Fiction" }
{ "title" : "Inglorius Basterds" }
{ "title" : "The Hobbit: An Unexpected Journey" }
{ "title" : "The Hobbit: The Desolation of smug" }
{ "title" : "The Hobbit: The Battle of the Five Armies" }
// Relationships
// Insert the following documents into a users collection
db.users.insertMany([{"username":"GoodGuyGreg","first_name":"Good Guy", "last_name" : "Greg"},{ "username" : "ScumbagSteve", "full_name" : {"first" : "Scumbag" , "last" : "Steve"}}]);_name":"Good Guy", "last_name" : "Greg"},{ "username" : "ScumbagSteve", "full_
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("6496d89b418592235b6e6594"),
ObjectId("6496d89b418592235b6e6595")
]
}
> db.users.find().pretty();
{
"_id" : ObjectId("6496d89b418592235b6e6594"),
"username" : "GoodGuyGreg",
"first_name" : "Good Guy",
"last_name" : "Greg"
}
{
"_id" : ObjectId("6496d89b418592235b6e6595"),
"username" : "ScumbagSteve",
"full_name" : {"first" : "Good Guy",
"last" : "Greg"
}
}
//Insert the following documents into a posts collection
db.posts.insertMany([{"username":"GoodGuyGreg","title" :" Passes out at party", "body" :" Wakes up early and cleans house"},{"username":"GoodGuyGreg","title" :"Steals your identity", "body" :"Raises your credit score"},{"username":"GoodGuyGreg","title" :" Reports a bug in your code", "body" :" Sends you a Pull Request"},{"username":"ScumbagSteve","title" :" Borrows something", "body" :"Sells it"},{"username":"ScumbagSteve","title" :"Borrows everything", "body" :"The end"},{"username":"ScumbagSteve","title" :"Forks your repo on github", "body" :"Sets to private"}]);orrows everything", "body" :"The end"},{"username":"ScumbagSteve","title" :"Forks your repo
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("6496d956418592235b6e6596"),
ObjectId("6496d956418592235b6e6597"),
ObjectId("6496d956418592235b6e6598"),
ObjectId("6496d956418592235b6e6599"),
ObjectId("6496d956418592235b6e659a"),
ObjectId("6496d956418592235b6e659b")
]
}
//Insert the following documents into a comments collection
> db.comments.insertMany([
... ... {
... ... username: "GoodGuyGreg",
... ... comment: "Hope you got a good deal!",
... ... post: ObjectId("6496d956418592235b6e6596")
... ... },
... ... {
... ... username: "GoodGuyGreg",
... ... comment: "Don't violate the licensing agreement!",
... ... post: ObjectId("6496d956418592235b6e6597")
... ... },
... ... {
... ... username: "GoodGuyGreg",
... ... comment: "Don't violate the licensing agreement!",
... ... post: ObjectId("6496d956418592235b6e6598")
... ... },
... ... {
... ... username: "ScumbagSteve",
... ... comment: "It still isn't clean",
... ... post: ObjectId("6496d956418592235b6e6599")
... ... },
... ... {
... ... username: "ScumbagSteve",
... ... comment: "Denied your PR cause I found a hack",
... ... post: ObjectId("6496d956418592235b6e659a")
... ... }
... ... ]);
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("6496dab7418592235b6e659c"),
ObjectId("6496dab7418592235b6e659d"),
ObjectId("6496dab7418592235b6e659e"),
ObjectId("6496dab7418592235b6e659f"),
ObjectId("6496dab7418592235b6e65a0")
]
}
// Querying related collections
// find all users
// find all posts
// find all posts that was authored by "GoodGuyGreg"
// find all posts that was authored by "ScumbagSteve"
// find all comments
// find all comments that was authored by "GoodGuyGreg"
// find all comments that was authored by "ScumbagSteve"
// find all comments belonging to the post "Reports a bug in your code"
> db.users.find().pretty();
{
"_id" : ObjectId("6496d89b418592235b6e6594"),
"username" : "GoodGuyGreg",
"first_name" : "Good Guy",
"last_name" : "Greg"
}
{
"_id" : ObjectId("6496d89b418592235b6e6595"),
"username" : "ScumbagSteve",
"full_name" : {
"first" : "Scumbag",
"last" : "Steve"
}
}
> db.posts.find().pretty();
{
"_id" : ObjectId("6496d956418592235b6e6596"),
"username" : "GoodGuyGreg",
"title" : " Passes out at party",
"body" : " Wakes up early and cleans house"
}
{
"_id" : ObjectId("6496d956418592235b6e6597"),
"username" : "GoodGuyGreg",
"title" : "Steals your identity",
"body" : "Raises your credit score"
}
{
"_id" : ObjectId("6496d956418592235b6e6598"),
"username" : "GoodGuyGreg",
"title" : " Reports a bug in your code",
"body" : " Sends you a Pull Request"
}
{
"_id" : ObjectId("6496d956418592235b6e6599"),
"username" : "ScumbagSteve",
"title" : " Borrows something",
"body" : "Sells it"
}
{
"_id" : ObjectId("6496d956418592235b6e659a"),
"username" : "ScumbagSteve",
"title" : "Borrows everything",
"body" : "The end"
}
{
"_id" : ObjectId("6496d956418592235b6e659b"),
"username" : "ScumbagSteve",
"title" : "Forks your repo on github",
"body" : "Sets to private"
}
> db.posts.find({ username: 'GoodGuyGreg' }).pretty();
{
"_id" : ObjectId("6496d956418592235b6e6596"),
"username" : "GoodGuyGreg",
"title" : " Passes out at party",
"body" : " Wakes up early and cleans house"
}
{
"_id" : ObjectId("6496d956418592235b6e6597"),
"username" : "GoodGuyGreg",
"title" : "Steals your identity",
"body" : "Raises your credit score"
}
{
"_id" : ObjectId("6496d956418592235b6e6598"),
"username" : "GoodGuyGreg",
"title" : " Reports a bug in your code",
"body" : " Sends you a Pull Request"
}
> db.posts.find({ username: 'ScumbagSteve' }).pretty();
{
"_id" : ObjectId("6496d956418592235b6e6599"),
"username" : "ScumbagSteve",
"title" : " Borrows something",
"body" : "Sells it"
}
{
"_id" : ObjectId("6496d956418592235b6e659a"),
"username" : "ScumbagSteve",
"title" : "Borrows everything",
"body" : "The end"
}
{
"_id" : ObjectId("6496d956418592235b6e659b"),
"username" : "ScumbagSteve",
"title" : "Forks your repo on github",
"body" : "Sets to private"
}
> db.comments.find().pretty();
{
"_id" : ObjectId("6496dab7418592235b6e659c"),
"username" : "GoodGuyGreg",
"comment" : "Hope you got a good deal!",
"post" : ObjectId("6496d956418592235b6e6596")
}
{
"_id" : ObjectId("6496dab7418592235b6e659d"),
"username" : "GoodGuyGreg",
"comment" : "Don't violate the licensing agreement!",
"post" : ObjectId("6496d956418592235b6e6597")
}
{
"_id" : ObjectId("6496dab7418592235b6e659e"),
"username" : "GoodGuyGreg",
"comment" : "Don't violate the licensing agreement!",
"post" : ObjectId("6496d956418592235b6e6598")
}
{
"_id" : ObjectId("6496dab7418592235b6e659f"),
"username" : "ScumbagSteve",
"comment" : "It still isn't clean",
"post" : ObjectId("6496d956418592235b6e6599")
}
{
"_id" : ObjectId("6496dab7418592235b6e65a0"),
"username" : "ScumbagSteve",
"comment" : "Denied your PR cause I found a hack",
"post" : ObjectId("6496d956418592235b6e659a")
}
> db.comments.find({ username: 'GoodGuyGreg' }).pretty();
{
"_id" : ObjectId("6496dab7418592235b6e659c"),
"username" : "GoodGuyGreg",
"comment" : "Hope you got a good deal!",
"post" : ObjectId("6496d956418592235b6e6596")
}
{
"_id" : ObjectId("6496dab7418592235b6e659d"),
"username" : "GoodGuyGreg",
"comment" : "Don't violate the licensing agreement!",
"post" : ObjectId("6496d956418592235b6e6597")
}
{
"_id" : ObjectId("6496dab7418592235b6e659e"),
"username" : "GoodGuyGreg",
"comment" : "Don't violate the licensing agreement!",
"post" : ObjectId("6496d956418592235b6e6598")
}
> db.comments.find({ username: 'ScumbagSteve' }).pretty();
{
"_id" : ObjectId("6496dab7418592235b6e659f"),
"username" : "ScumbagSteve",
"comment" : "It still isn't clean",
"post" : ObjectId("6496d956418592235b6e6599")
}
{
"_id" : ObjectId("6496dab7418592235b6e65a0"),
"username" : "ScumbagSteve",
"comment" : "Denied your PR cause I found a hack",
"post" : ObjectId("6496d956418592235b6e659a")
}
> db.comments.find({ _id: ObjectId("62b2131f58e2771bd28580ef") }).pretty();
> db.comments.find().pretty();
{
"_id" : ObjectId("6496dab7418592235b6e659c"),
"username" : "GoodGuyGreg",
"comment" : "Hope you got a good deal!",
"post" : ObjectId("6496d956418592235b6e6596")
}
{
"_id" : ObjectId("6496dab7418592235b6e659d"),
"username" : "GoodGuyGreg",
"comment" : "Don't violate the licensing agreement!",
"post" : ObjectId("6496d956418592235b6e6597")
}
{
"_id" : ObjectId("6496dab7418592235b6e659e"),
"username" : "GoodGuyGreg",
"comment" : "Don't violate the licensing agreement!",
"post" : ObjectId("6496d956418592235b6e6598")
}
{
"_id" : ObjectId("6496dab7418592235b6e659f"),
"username" : "ScumbagSteve",
"comment" : "It still isn't clean",
"post" : ObjectId("6496d956418592235b6e6599")
}
{
"_id" : ObjectId("6496dab7418592235b6e65a0"),
"username" : "ScumbagSteve",
"comment" : "Denied your PR cause I found a hack",
"post" : ObjectId("6496d956418592235b6e659a")
}
> db.posts.find().pretty();
{
"_id" : ObjectId("6496d956418592235b6e6596"),
"username" : "GoodGuyGreg",
"title" : " Passes out at party",
"body" : " Wakes up early and cleans house"
}
{
"_id" : ObjectId("6496d956418592235b6e6597"),
"username" : "GoodGuyGreg",
"title" : "Steals your identity",
"body" : "Raises your credit score"
}
{
"_id" : ObjectId("6496d956418592235b6e6598"),
"username" : "GoodGuyGreg",
"title" : " Reports a bug in your code",
"body" : " Sends you a Pull Request"
}
{
"_id" : ObjectId("6496d956418592235b6e6599"),
"username" : "ScumbagSteve",
"title" : " Borrows something",
"body" : "Sells it"
}
{
"_id" : ObjectId("6496d956418592235b6e659a"),
"username" : "ScumbagSteve",
"title" : "Borrows everything",
"body" : "The end"
}
{
"_id" : ObjectId("6496d956418592235b6e659b"),
"username" : "ScumbagSteve",
"title" : "Forks your repo on github",
"body" : "Sets to private"
}
> db.comments.updateOne({"username":"ScumbagSteve"},{$set:{"post":ObjectId("6496d956418592235b6e6598")}});
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
> db.comments.updateOne({"username":"GoodGuyGreg"},{$set:{"post":ObjectId("6496d956418592235b6e6598")}});
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
> db.comments.find({ _id: ObjectId("62b2131f58e2771bd28580ef") }).pretty();
> db.comments.find({ _id: ObjectId("6496d956418592235b6e6598") }).pretty();
> db.comments.find({ post: ObjectId("6496d956418592235b6e6598") }).pretty();
{
"_id" : ObjectId("6496dab7418592235b6e659c"),
"username" : "GoodGuyGreg",
"comment" : "Hope you got a good deal!",
"post" : ObjectId("6496d956418592235b6e6598")
}
{
"_id" : ObjectId("6496dab7418592235b6e659e"),
"username" : "GoodGuyGreg",
"comment" : "Don't violate the licensing agreement!",
"post" : ObjectId("6496d956418592235b6e6598")
}
{
"_id" : ObjectId("6496dab7418592235b6e659f"),
"username" : "ScumbagSteve",
"comment" : "It still isn't clean",
"post" : ObjectId("6496d956418592235b6e6598")
} |
db.movies.insertOne({
"title" : "Fight Club",
"writer" : "Chuck Palahniuk",
"year" : 1999,
"actors" : [
"Brad Pitt",
"Edward Norton",
],
});db.movies.insertMany([
{
"title" : "Pulp Fiction",
"writer" : "Quentin Tarantino",
"year" : 1994,
"actors" : [
"John Travolta",
"Uma Thurman",
],
},
{
"title" : "Inglorious Basterds",
"writer" : "Quentin Tarantino",
"year" : 2009,
"actors" : [
"Brad Pitt",
"Diane Kruger",
"Eli Roth",
],
}
]);Query / Find Documentsquery the movies collection to
db.movies.find({})
db.movies.find({writer:"Quentin Tarantino"})
db.movies.find({ actors: { $in: ["Brad Pitt"] } })
db.movies.find( { franchise : "The Hobbit" })
db.movies.find({ $and : [ {year: {$gte: 1990}}, {year: {$lte: 1999}} ] })
db.movies.find({ $or : [ {year : { $gt :2010 }}, { year :{ $lt : 2000 }} ] }) |
|
// 1. get all documents |
|
//get all documents with writer set to "Quentin Tarantino" |






















































Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
MongoDB Practice
MongoDB Exercise in mongo shell
Connect to a running mongo instance, use a database named
mongo_practice.Document all your queries in a javascript file to use as a reference.
Insert Documents
Insert the following documents into a
moviescollection.Query / Find Documents
query the
moviescollection towriterset to "Quentin Tarantino"actorsinclude "Brad Pitt"franchiseset to "The Hobbit"Update Documents
Text Search
Delete Documents
Relationships
Insert the following documents into a
userscollectionInsert the following documents into a
postscollectionInsert the following documents into a
commentscollectionwhere [post_obj_id] is the ObjectId of the
postsdocument: "Borrows something"where [post_obj_id] is the ObjectId of the
postsdocument: "Borrows everything"where [post_obj_id] is the ObjectId of the
postsdocument: "Forks your repo on githubwhere [post_obj_id] is the ObjectId of the
postsdocument: "Passes out at party"where [post_obj_id] is the ObjectId of the
postsdocument: "Reports a bug in your code"Querying related collections
All reactions