diff --git a/1/1_2.sh b/1/1_2.sh index b8a3b4f..d3d9d55 100644 --- a/1/1_2.sh +++ b/1/1_2.sh @@ -1,4 +1,3 @@ - npm version npm install -g m m latest @@ -7,4 +6,4 @@ brew tap mongodb/brew brew install mongosh mongosh --version brew install mongodb-database-tools -mongoimport +mongoimport \ No newline at end of file diff --git a/1/1_3.sh b/1/1_3.sh index ae8c559..10f3c31 100644 --- a/1/1_3.sh +++ b/1/1_3.sh @@ -2,6 +2,4 @@ npm version sudo npm install -g m m latest mongod --version -docs.mongodb.com/mongodb-shell/install/ - -sudo apt-get install mongodb-database-tools +sudo apt-get install mongodb-database-tools \ No newline at end of file diff --git a/1/1_4.sh b/1/1_4.sh index ae8c559..10f3c31 100644 --- a/1/1_4.sh +++ b/1/1_4.sh @@ -2,6 +2,4 @@ npm version sudo npm install -g m m latest mongod --version -docs.mongodb.com/mongodb-shell/install/ - -sudo apt-get install mongodb-database-tools +sudo apt-get install mongodb-database-tools \ No newline at end of file diff --git a/1/2_1.sh b/1/2_1.sh deleted file mode 100644 index ae8c559..0000000 --- a/1/2_1.sh +++ /dev/null @@ -1,7 +0,0 @@ -npm version -sudo npm install -g m -m latest -mongod --version -docs.mongodb.com/mongodb-shell/install/ - -sudo apt-get install mongodb-database-tools diff --git a/1/2_3.sh b/1/2_3.sh deleted file mode 100644 index ae8c559..0000000 --- a/1/2_3.sh +++ /dev/null @@ -1,7 +0,0 @@ -npm version -sudo npm install -g m -m latest -mongod --version -docs.mongodb.com/mongodb-shell/install/ - -sudo apt-get install mongodb-database-tools diff --git a/2/2_1.sh b/2/2_1.sh new file mode 100644 index 0000000..e31f921 --- /dev/null +++ b/2/2_1.sh @@ -0,0 +1,7 @@ +mkdir mongodbessentials +cd mongodbessentials +mkdir mongod_only +mongod --dbpath mongod_only +mongosh +show databases +db.test.insertOne({"hello": "world"}) \ No newline at end of file diff --git a/2/2_3.sh b/2/2_3.sh new file mode 100644 index 0000000..c3853ac --- /dev/null +++ b/2/2_3.sh @@ -0,0 +1,18 @@ +mkdir replica_set_cmdline +cd replica_set_cmdline +openssl rand -base64 755 > keyfile +chmod 400 keyfile +mkdir -p m{1,2,3}/db +mongod --replSet myReplSet --dbpath ./m1/db --logpath ./m1/mongodb.log --port 27017 --fork --keyFile ./keyfile +mongod --replSet myReplSet --dbpath ./m2/db --logpath ./m2/mongodb.log --port 27018 --fork --keyFile ./keyfile +mongod --replSet myReplSet --dbpath ./m3/db --logpath ./m3/mongodb.log --port 27019 --fork --keyFile ./keyfile +mongosh “mongodb://localhost:27017” +rs.initiate() +use admin +db.createUser({user: 'naomi', pwd: passwordPrompt(), roles: ["root"]}) +db.getSiblingDB("admin").auth("naomi", passwordPrompt()) +rs.add("localhost:27018") +rs.add("localhost:27019") +rs.status() +db.serverStatus()['repl'] +rm -rf mongodbessentials \ No newline at end of file diff --git a/2/2_4.sh b/2/2_4.sh new file mode 100644 index 0000000..ae30424 --- /dev/null +++ b/2/2_4.sh @@ -0,0 +1,28 @@ +mkdir replicaset +cd replicaset +openssl rand -base64 755 > keyfile +chmod 400 keyfile +mkdir -p m{1,2,3}/db +touch m1.conf +cp m1.conf m2.conf +cp m1.conf m3.conf +mongod -f m1.conf +mongod -f m2.conf +mongod -f m3.conf +mongosh +use admin +config = { + _id: "mongodb-essential-rs", + members: [ + { _id: 0, host: "localhost:27017" }, + { _id: 1, host: "localhost:27018" }, + { _id: 2, host: "localhost:27019" } + ] +}; +rs.initiate(config) +db.createUser({user: 'naomi', pwd: passwordPrompt(), roles: ["root"]}) +db.getSiblingDB("admin").auth("naomi") +rs.add("localhost:27018") +rs.add("localhost:27019") +rs.status() +db.serverStatus()['repl'] \ No newline at end of file diff --git a/2/2_5.sh b/2/2_5.sh new file mode 100644 index 0000000..db07f79 --- /dev/null +++ b/2/2_5.sh @@ -0,0 +1,4 @@ +cd datasets +mongoimport --username="naomi" --authenticationDatabase="admin" --db=sample_data inventory.json +mongoimport --username="naomi" --authenticationDatabase="admin" --db=sample_data movies.json +mongoimport --username="naomi" --authenticationDatabase="admin" --db=sample_data orders.json \ No newline at end of file diff --git a/2/m1.conf b/2/m1.conf new file mode 100644 index 0000000..9474282 --- /dev/null +++ b/2/m1.conf @@ -0,0 +1,15 @@ +storage: + dbPath: m1/db +net: + bindIp: localhost + port: 27017 +security: + authorization: enabled + keyFile: keyfile +systemLog: + destination: file + path: m1/mongod.log +processManagement: + fork: true +replication: + replSetName: mongodb-essentials-rs \ No newline at end of file diff --git a/2/m2.conf b/2/m2.conf new file mode 100644 index 0000000..0135773 --- /dev/null +++ b/2/m2.conf @@ -0,0 +1,15 @@ +storage: + dbPath: m2/db +net: + bindIp: localhost + port: 27018 +security: + authorization: enabled + keyFile: keyfile +systemLog: + destination: file + path: m2/mongod.log +processManagement: + fork: true +replication: + replSetName: mongodb-essentials-rs \ No newline at end of file diff --git a/2/m3.conf b/2/m3.conf new file mode 100644 index 0000000..189b6a8 --- /dev/null +++ b/2/m3.conf @@ -0,0 +1,15 @@ +storage: + dbPath: m3/db +net: + bindIp: localhost + port: 27019 +security: + authorization: enabled + keyFile: keyfile +systemLog: + destination: file + path: m3/mongod.log +processManagement: + fork: true +replication: + replSetName: mongodb-essentials-rs \ No newline at end of file diff --git a/3/3_2.sh b/3/3_2.sh new file mode 100644 index 0000000..4bf1100 --- /dev/null +++ b/3/3_2.sh @@ -0,0 +1,3 @@ +use blog +show collections +db.authors.insertOne({"name": "Naomi Pentrel"}) \ No newline at end of file diff --git a/3/3_3.sh b/3/3_3.sh new file mode 100644 index 0000000..6181def --- /dev/null +++ b/3/3_3.sh @@ -0,0 +1,20 @@ +db.authors.insertOne({"name": "Joe Nash"}) +db.authors.insertMany([ + {"name": "Eliot Horowitz"}, + {"name": "Dwight Merriman"}, + {"name": "Kevin P. Ryan"} +]) +db.authors.find() +db.authors.find({ "name": "Naomi Pentrel"}) +db.authors.find({ name: "Naomi Pentrel"}) +db.authors.updateOne( + { name: "Naomi Pentrel"}, + { $set: { website: "https://naomi.codes" } } +) +db.authors.find({ name: "Naomi Pentrel"}) +db.authors.updateMany( + { }, + { $set: { books: [] } } +) +db.authors.find() +db.authors.deleteOne({ name: "Joe Nash" }) \ No newline at end of file diff --git a/3/3_4.sh b/3/3_4.sh new file mode 100644 index 0000000..9b34de0 --- /dev/null +++ b/3/3_4.sh @@ -0,0 +1,2 @@ +db.authors.find() +db.createIndex({ name: 1 }) \ No newline at end of file diff --git a/3/3_6.sh b/3/3_6.sh new file mode 100644 index 0000000..626c80a --- /dev/null +++ b/3/3_6.sh @@ -0,0 +1,10 @@ +use lookup +db.records.insert({ + "name": "Naomi Pentrel", + "number": "1234567890", + "profession": "LinkedIn Learning Instructor", + "website": "naomi.codes" +}) +db.records.findOne() +db.records.createIndex({name: 1}) +db.records.createIndex({number: 1}) \ No newline at end of file diff --git a/3/script.sh b/3/script.sh deleted file mode 100644 index e69de29..0000000 diff --git a/4/4_11.sh b/4/4_11.sh new file mode 100644 index 0000000..b2ba0ab --- /dev/null +++ b/4/4_11.sh @@ -0,0 +1,67 @@ +use order_app +db.oders.insertMany([ + { + "time": Date(), + "items": [{ + "name": "Fries", + "quantity": 1, + "price": 2.99 + },{ + "name": "Diet Coke", + "quantity": 1, + "price": 1.99 + }], + "delivery_cost": 3.50, + "total": 8.48, + "user": { + "name": "Naomi Pentrel", + "address": "Sample Street 123", + "email": "naomi@email.com", + "phone": "12345678910", + "balance": 10 + }, + "restaurant": { + "name": "Burger King", + "address": "Somewhere in Amsterdam" + } + }, + { + "time": Date(), + "items": [{ + "name": "Fries", + "quantity": 1, + "price": 2.99 + },{ + "name": "Diet Coke", + "quantity": 2, + "price": 1.99 + }], + "delivery_cost": 3.50, + "total": 10.47, + "user": { + "name": "Naomi Pentrel", + "address": "Sample Street 123", + "email": "naomi@email.com", + "phone": "12345678910", + "balance": 10 + }, + "restaurant": { + "name": "Burger King", + "address": "Somewhere in Amsterdam" + } + } +]) +db.orders.findOne() +db.orders.createIndex({"user.email": 1, time: 1}) +db.orders.createIndex({"restaurant.name": 1, time: 1}) +db.orders.find().sort({total: -1}).limit(1) +db.orders.find().sort({"restaurant.name": 1}) +db.orders.find({ + $expr: { + $gt: [ + "$total", + "$user.balance" + ] + } +}) +db.orders.updateMany({}, { $mul: {total: 0.9} }) diff --git a/4/4_2.sh b/4/4_2.sh new file mode 100644 index 0000000..98c9869 --- /dev/null +++ b/4/4_2.sh @@ -0,0 +1,7 @@ +use sample_data +show collections +db.inventory.findOne() +db.orders.findOne() +db.movies.findOne() +db.movies.findOne({"ratings.mndb": 10}) +db.movies.findOne({"genres.0": "Musical"}) \ No newline at end of file diff --git a/4/4_3.sh b/4/4_3.sh new file mode 100644 index 0000000..028eaa4 --- /dev/null +++ b/4/4_3.sh @@ -0,0 +1,11 @@ +use sample_data +db.inventory.findOne() +db.inventory.findOne({ "variations.quantity": { $gte: 8 } }) +db.inventory.findOne({ "variations.quantity": { $gte: 17 } }) +db.inventory.findOne({ "variations.quantity": { $gte: 18 } }) +db.inventory.findOne({ "variations.quantity": { $gte: 22 } }) +db.inventory.findOne({ "price": { $lt: 1000 } }) +db.inventory.findOne({ "price": { $lt: 2000 } }) +db.inventory.findOne({ "price": { $lt: 1700 } }) +db.inventory.findOne({ "variations.variation": { $in: [ "Blue", "Red" ] } }) +db.inventory.findOne({ "variations.variation": { $nin: [ "Blue", "Red" ] } }) \ No newline at end of file diff --git a/4/4_4.sh b/4/4_4.sh new file mode 100644 index 0000000..eb0d127 --- /dev/null +++ b/4/4_4.sh @@ -0,0 +1,32 @@ +use sample_data +db.inventory.findOne() +db.inventory.findOne( + { + $and: [ + { "variations.quantity": { $ne: 0} }, + { "variations.quantity": { $exists: true } } + ] + } +) +db.inventory.findOne( + { + $or: [ + { "variations.variation": "Blue" }, + { "variations.variation": "Green" } + { "variations.variation": "Teal" }, + ] + } +) +db.inventory.findOne( + { + $nor: [ + { price: { $gt: 8000 } }, + { "variations.variation": "Blue" } + ] + } +) +db.inventory.findOne( + { + $not: { price: { $gt: 20000 } }, + } +) \ No newline at end of file diff --git a/4/4_5.sh b/4/4_5.sh new file mode 100644 index 0000000..b2e16ff --- /dev/null +++ b/4/4_5.sh @@ -0,0 +1,6 @@ +db.movies.find().sort({title: 1}) +db.movies.find({}, { title: 1, genres: 1 }).sort( {title: 1}) +db.movies.find({}, { title: 1, genres: 1 }).sort( {title: -1}) +db.movies.find({}, { title: 1, genres: 1 }).sort( {director: 1, title: 1}) +db.movies.find({}, { title: 1, genres: 1 }).sort( {title: 1}).skip(100) +db.movies.find({}, { title: 1, genres: 1 }).sort( {title: 1}).skip(100).limit(5) diff --git a/4/4_6.sh b/4/4_6.sh new file mode 100644 index 0000000..05c4941 --- /dev/null +++ b/4/4_6.sh @@ -0,0 +1,18 @@ +use blog +show collections +db.authors.find() +db.authors.updateOne( + { name: "Naomi Pentrel" }, + { $set: { message: "Hello World!" } } +) +db.authors.find() +db.authors.updateMany( + {}, + { $set: { message: "Hello" } } +) +db.authors.find() +db.authors.updateMany( + {}, + { $unset: { message: "" } } +) +db.authors.find() diff --git a/4/4_7.sh b/4/4_7.sh new file mode 100644 index 0000000..6cd696b --- /dev/null +++ b/4/4_7.sh @@ -0,0 +1,62 @@ +use sample_data +db.movies.findOne() +db.movies.find({genres: "Comedy"}) +db.movies.find({genres: ["Comedy", "Drama", "Thriller"]}) +db.movies.find({genres: { $all: ["Comedy", "Drama"] } }) +db.inventory.findOne() +db.inventory.find( + { + variations: { + $elemMatch: { + variation: "Blue", + quantity: { $gt: 8 } + } + } + } +) +db.movies.findOne() +db.movies.updateOne( + { + title: 'The Adventures of Tom Thumb & Thumbelina' + }, + { + $push: { genres: "Test" } + } +) +db.movies.findOne( {title: 'The Adventures of Tom Thumb & Thumbelina'} ) +db.movies.updateOne( + { + title: 'The Adventures of Tom Thumb & Thumbelina' + }, + { + $addToSet: { genres: "Test" } + } +) +db.movies.findOne( {title: 'The Adventures of Tom Thumb & Thumbelina'} ) +db.movies.updateOne( + { + title: 'The Adventures of Tom Thumb & Thumbelina' + }, + { + $addToSet: { genres: "Green" } + } +) +db.movies.findOne( {title: 'The Adventures of Tom Thumb & Thumbelina'} ) +db.movies.updateOne( + { + title: 'The Adventures of Tom Thumb & Thumbelina' + }, + { + $pop: { genres: 1 } + } +) +db.movies.findOne( {title: 'The Adventures of Tom Thumb & Thumbelina'} ) +db.movies.updateOne( + { + title: 'The Adventures of Tom Thumb & Thumbelina' + }, + { + $pop: { genres: 1 } + } +) +db.movies.findOne( {title: 'The Adventures of Tom Thumb & Thumbelina'} ) diff --git a/4/4_8.sh b/4/4_8.sh new file mode 100644 index 0000000..a22d0b9 --- /dev/null +++ b/4/4_8.sh @@ -0,0 +1,7 @@ +use blog +session = db.getMongo().startSession( { readPreference: { mode: "primary" } } ) +session.startTransaction() +session.getDatabase("blog").updateMany( {}, {$set: {message: "Transaction occured"} } ) +session.commitTransaction() +session.endSession() +db.authors.find() \ No newline at end of file diff --git a/4/4_9.sh b/4/4_9.sh new file mode 100644 index 0000000..df49b45 --- /dev/null +++ b/4/4_9.sh @@ -0,0 +1,6 @@ +use sample_data +db.movies.findOne() +db.movies.find({ title: 1, ratings: 1 }) +db.movies.find( + { title: 1, ratings: 1 } +) diff --git a/4/script.sh b/4/script.sh deleted file mode 100644 index e69de29..0000000 diff --git a/5/5_10.sh b/5/5_10.sh new file mode 100644 index 0000000..a7ece81 --- /dev/null +++ b/5/5_10.sh @@ -0,0 +1,18 @@ +db.mock_movie_data.aggregate([ + { $project: { + release_year: { $year: "$release_year" }, + genres: 1, + runtime_min: 1, + title: 1 + } }, + { $match: { release_year: { $gte: 2000 } } }, + { $unwind: "$genres"}, + { $group: { + _id: { + genres: "$genres", + }, + movies: { $push: { title: "$title", runtime_min: "$runtime_min", release_year: "$release_year"} }, + count: { $sum: 1 } + } }, + { $out: { db: "sample_data", coll: "genres2000" } } +]) \ No newline at end of file diff --git a/5/5_2.sh b/5/5_2.sh new file mode 100644 index 0000000..6dd3ec2 --- /dev/null +++ b/5/5_2.sh @@ -0,0 +1,36 @@ +use sample_data +db.inventory.findOne() +db.inventory.aggregate( [ + { + $group: { + _id: "$source", + } + } +] ) +db.inventory.aggregate( [ + { + $group: { + _id: "$source", + count: { $sum: 1 } + } + } +] ) +db.inventory.aggregate( [ + { + $group: { + _id: "$source", + count: { $sum: 1 }, + items: { $push: "$name" } + } + } +] ) +db.inventory.aggregate( [ + { + $group: { + _id: "$source", + count: { $sum: 1 }, + items: { $push: "$name" }, + avg_price: { $avg: "$price"} + } + } +] ) \ No newline at end of file diff --git a/2/script.sh b/5/5_3.sh similarity index 100% rename from 2/script.sh rename to 5/5_3.sh diff --git a/5/5_4.sh b/5/5_4.sh new file mode 100644 index 0000000..4fe9cbd --- /dev/null +++ b/5/5_4.sh @@ -0,0 +1,26 @@ +db.inventory.findOne() +db.inventory.aggregate( [ + { + $unwind: "$variations" + } +] ) +db.inventory.findOne() +db.inventory.aggregate( [ + { + $unwind: "$variations", + }, + { + $match: { "variations.variation": "Purple" } + } +] ) +db.inventory.aggregate( [ + { + $match: { "variations.variation": "Purple" } + }, + { + $unwind: "$variations", + }, + { + $match: { "variations.variation": "Purple" } + } +] ) diff --git a/5/5_5.sh b/5/5_5.sh new file mode 100644 index 0000000..2ac53c8 --- /dev/null +++ b/5/5_5.sh @@ -0,0 +1,35 @@ +db.inventory.aggregate( [ + { + $match: { "variations.variation": "Purple" } + }, + { + $unwind: "$variations", + }, + { + $match: { "variations.variation": "Purple" } + }, + { + $out: { db: "sample_data", coll: "purple" } + } +] ) +show collections +db.purple.find() +db.inventory.aggregate( [ + { + $match: { "variations.variation": "Purple" } + }, + { + $unwind: "$variations", + }, + { + $match: { "variations.variation": "Purple" } + }, + { + $merge: { + into: "purple", + on: "_id", + whenMatched: "keepExisting" + whenNotMatched: "insert" + } + } +] ) diff --git a/5/5_6.sh b/5/5_6.sh new file mode 100644 index 0000000..d13545a --- /dev/null +++ b/5/5_6.sh @@ -0,0 +1,15 @@ +db.movies.findOne() +db.movies.aggregate([ + { + $project: { + title: 1, + actors: { + $function: { + body: 'function(actors) { return actors.sort(); }', + args: [ "$actors" ], + lang: "js" + } + } + } + } +]) \ No newline at end of file diff --git a/5/5_7.sh b/5/5_7.sh new file mode 100644 index 0000000..0ab148d --- /dev/null +++ b/5/5_7.sh @@ -0,0 +1,8 @@ +db.orders.aggregate([ + { $lookup: { + from: "inventory", + localField: "car_id", + foreignField: "_id", + as: "car_id" + } } +]) \ No newline at end of file diff --git a/5/5_8.sh b/5/5_8.sh new file mode 100644 index 0000000..6c4c315 --- /dev/null +++ b/5/5_8.sh @@ -0,0 +1,12 @@ +db.movies.explain("executionStats").aggregate( [ + { $project: { + release_year: {$year: "$release_year"}, + title: 1 + } }, + { $lookup: { + from: "inventory", + localField: "release_year", + foreignField: "year", + as: "year" + } } +] ) \ No newline at end of file diff --git a/5/script.sh b/5/script.sh deleted file mode 100644 index e69de29..0000000 diff --git a/6/6_3.sh b/6/6_3.sh new file mode 100644 index 0000000..4bf1100 --- /dev/null +++ b/6/6_3.sh @@ -0,0 +1,3 @@ +use blog +show collections +db.authors.insertOne({"name": "Naomi Pentrel"}) \ No newline at end of file diff --git a/6/script.sh b/6/script.sh deleted file mode 100644 index e69de29..0000000