Here are my solutions to the “Optional Tasks”.
Task 1. Rename the listingsAndReviews collection to a name of your choice.
show dbs
use restaurants
show collections
Use this operation to rename the collection to “listings":
db.listingsAndReviews.renameCollection("listings")
Task 2. Update all the restaurants from your favorite NYC borough by removing the grades field. They are all a “A” in your eyes!
db.listings.updateMany({ borough: "Manhattan"}, {$unset: {"grades": ""}} )
The array fields named “grades" has now been deleted for all restaurants in Manhattan.
Tasks 3. Use .bulkWrite() to perform the following operations in any order: - Add three documents using insertOne. - Update two documents using updateMany. - Replaces a document using replaceOne.
This task was very tough for me.
Here is how I worked my way through it.
I found the syntax for the .bulkWrite() method at the MongoDB website. I use this page as a reference for the syntax:
db.collection.bulkWrite() — MongoDB Manual
https://www.mongodb.com/docs/manual/reference/method/db.collection.bulkWrite/#definition
Syntax for bulkWrite() operaton:
db.collection.bulkWrite(
[ <operation 1>, <operation 2>, ... ]
)
So, based on the tasks in the exercise the overall structure of the bulkWrite operation looks like this:
db.collection.bulkWrite(
[ insertOne(), insertOne(), insertOne(), updateMany(), replaceOne() ]
)
I started with creating each individual operation (insertOne, updateMany, replaceOne) and tested each of them in the Codecademy command line.
I also tested them individually with the bulkWrite() method.
Finally, I put all the operations together into one big bulkWrite() operation.
insertOne()
The MongoDB code looks like this:
{ insertOne: { address: { building: "111", coord: [ 11.11, 11.11 ], street: "1 Avenue", zipcode: "11111" }, borough: "Manhattan", cuisine: "Mexican", name: "1 Place", restaurant_id: "11111111" }
},
{ insertOne: { address: { building: "222", coord: [ 22.22, 22.22 ], street: "2 Avenue", zipcode: "22222" }, borough: "Manhattan", cuisine: "Mexican", name: "2 Place", restaurant_id: "22222222" }
},
{ insertOne: { address: { building: "333", coord: [ 33.33, 33.33 ], street: "3 Avenue", zipcode: "33333" }, borough: "Manhattan", cuisine: "Mexican", name: "3 Place", restaurant_id: "33333333" }
}
Testing insertOne() in command line in Codecademy with 1 restaurant:
db.listings.insertOne({ address: { building: "111", coord: [ 11.11, 11.11 ], street: "1 Avenue", zipcode: "11111" }, borough: "Manhattan", cuisine: "Mexican", name: "1 Place", restaurant_id: "11111111" })
Check if it was created correctly with this command:
listings.find({ name: "1 Place" })
Testing in command line in Codecademy with bulkWrite() with 3 restaurants:
db.listings.bulkWrite( [ { insertOne: { address: { building: "111", coord: [ 11.11, 11.11 ], street: "1 Avenue", zipcode: "11111" }, borough: "Manhattan", cuisine: "Mexican", name: "1 Place", restaurant_id: "11111111" }}, { insertOne: { address: { building: "222", coord: [ 22.22, 22.22 ], street: "2 Avenue", zipcode: "22222" }, borough: "Manhattan", cuisine: "Mexican", name: "2 Place", restaurant_id: "22222222" }}, { insertOne: { address: { building: "333", coord: [ 33.33, 33.33 ], street: "3 Avenue", zipcode: "33333" }, borough: "Manhattan", cuisine: "Mexican", name: "3 Place", restaurant_id: "33333333" } } ] )
The code will create 3 new restaurants.
You can use this code to see them:
db.listings.find({ name: { "$in" : [ "1 Place", "2 Place", "3 Place" ] }})
updateMany()
db.listings.updateMany( { name: { "$in" : [ "Cafe Bar", "Wakamba" ] } }, { $set: { cuisine: “Grill“ } } );
This code will change the cuisine to “Grill" in the two restaurants named “Cafe Bar" and “Wakamba".
And with bulkWrite():
db.listings.bulkWrite( [ { updateMany : { "filter" : { name: { "$in" : [ "Dudes", "Wakamba" ] } }, "update" : { $set: { cuisine: "Thai" }} } } ] );
You can use this code to see the change:
db.listings.find({ name: { "$in" : [ "Cafe Bar", "Wakamba" ] }})
Be aware that when you use the updateMany() method with the bulkWrite method you must use the fields “filter" and “update":
db.collection.bulkWrite( [ { updateMany : { "filter" : <document>, "update" : <document or pipeline>, // Changed in MongoDB 4.2 "upsert" : <boolean>, "collation": <document>, // Available starting in 3.4 "arrayFilters": [ <filterdocument1>, ... ], // Available starting in 3.6 "hint": <document|string> // Available starting in 4.2.1 } } ] )
This was very confusing for me until I found it in the documentation:
db.collection.bulkWrite() — MongoDB Manual
https://www.mongodb.com/docs/manual/reference/method/db.collection.bulkWrite/#std-label-bulkwrite-write-operations-updateOneMany
replaceOne()
replaceOne() replace the entire document, while updateOne() allows for updating fields. Since replaceOne() replaces the entire document - fields in the old document not contained in the new one will be lost - only the new fields are added.
This code will replace the document with the restaurant name “Mozzarella" with a new document where the name is “New Mozzarella" and a new field called “taste" with the value “Juicy".
db.listings.replaceOne( { name: "Mozzarella" }, { replacement: { name: "New Mozzarella", taste: "Juicy" } } );
Now with the bulkWrite method:
db.listings.bulkWrite( [ { replaceOne: { filter: { name: "Mozzarella" }, replacement: {name: "New Mozzarella", taste: "Juicy"}}} ] );
Again, you must use the fields “filter" and “replacement" (not “update") when replaceOne() is used together with bulkWrite().
You can use this code to see the change made to the document:
db.listings.find({ name: "New Mozzarella"})
Now it is time to combine all the code to form one big bulkWrite() operation that comprises all 5 write operations in the above:
db.listings.bulkWrite(
[
{ insertOne :
{
address: { building: "111", coord: [ 11.11, 11.11 ], street: "1 Avenue", zipcode: "11111" }, borough: "Manhattan", cuisine: "Mexican", name: "1 Place", restaurant_id: "11111111"
}
},
{ insertOne :
{
address: { building: "222", coord: [ 22.22, 22.22 ], street: "2 Avenue", zipcode: "22222" }, borough: "Manhattan", cuisine: "Mexican", name: "2 Place", restaurant_id: "22222222"
}
},
{ insertOne :
{
address: { building: "333", coord: [ 33.33, 33.33 ], street: "3 Avenue", zipcode: "33333" }, borough: "Manhattan", cuisine: "Mexican", name: "3 Place", restaurant_id: "33333333"
}
},
{ updateMany :
{
"filter" : { name: { "$in" : [ "Cafe Bar", "Wakamba" ] } },
"update" : { $set: { cuisine: "Grill" }}
}
},
{ replaceOne :
{
"filter" : { name: "Mozzarella" },
"replacement" : { name: "New Mozzarella", taste: "Juicy" }
}
}
]
)
You can copy-paste the following code block to test it in the command line in Codecademy (I never found out how to write multi-line code in the Codecademy command line).
Before you do remember to reset the exercise.
Click “Get Unstuck>Reset Exercise>Reset"
Plus, also make sure that the name of the collection is changed to “listings".
db.listings.bulkWrite( [ { insertOne: { address: { building: "111", coord: [ 11.11, 11.11 ], street: "1 Avenue", zipcode: "11111" }, borough: "Manhattan", cuisine: "Mexican", name: "1 Place", restaurant_id: "11111111" }}, { insertOne: { address: { building: "222", coord: [ 22.22, 22.22 ], street: "2 Avenue", zipcode: "22222" }, borough: "Manhattan", cuisine: "Mexican", name: "2 Place", restaurant_id: "22222222" }}, { insertOne: { address: { building: "333", coord: [ 33.33, 33.33 ], street: "3 Avenue", zipcode: "33333" }, borough: "Manhattan", cuisine: "Mexican", name: "3 Place", restaurant_id: "33333333" } }, { updateMany : { "filter" : { name: { "$in" : [ "Cafe Bar", "Wakamba" ] } }, "update" : { $set: { cuisine: "Grill" }} } }, { replaceOne: { filter: { name: "Mozzarella" }, replacement: {name: "New Mozzarella", taste: "Juicy"}}} ] )
Check that it worked with the following commands.
Command: db.listings.find({ name: "New Mozzarella"})
Output:
[
{
_id: ObjectId("5eb3d669b31de5d588f47d6e"),
name: 'New Mozzarella',
taste: 'Juicy'
}
]
Command: db.listings.find({ name: { "$in" : [ "Cafe Bar", "Wakamba" ] }}, {grades: 0, address:0})
Output:
[
{
_id: ObjectId("5eb3d668b31de5d588f43081"),
borough: 'Manhattan',
cuisine: 'Grill',
name: 'Wakamba',
restaurant_id: '40564625'
},
{
_id: ObjectId("5eb3d668b31de5d588f4305b"),
borough: 'Queens',
cuisine: 'Grill',
name: 'Cafe Bar',
restaurant_id: '40561796'
}
]
Command: db.listings.find({ name: { "$in" : [ "1 Place", "2 Place", "3 Place" ] }})
Output:
[
{
_id: ObjectId("6465cf28ebdaa1826aa08000"),
address: {
building: '111',
coord: [ 11.11, 11.11 ],
street: '1 Avenue',
zipcode: '11111'
},
borough: 'Manhattan',
cuisine: 'Mexican',
name: '1 Place',
restaurant_id: '11111111'
},
{
_id: ObjectId("6465cf28ebdaa1826aa08001"),
address: {
building: '222',
coord: [ 22.22, 22.22 ],
street: '2 Avenue',
zipcode: '22222'
},
borough: 'Manhattan',
cuisine: 'Mexican',
name: '2 Place',
restaurant_id: '22222222'
},
{
_id: ObjectId("6465cf28ebdaa1826aa08002"),
address: {
building: '333',
coord: [ 33.33, 33.33 ],
street: '3 Avenue',
zipcode: '33333'
},
borough: 'Manhattan',
cuisine: 'Mexican',
name: '3 Place',
restaurant_id: '33333333'
}
]