Hi, I’m taking M001:Mongodb basic courses and I’m at chapter 3 Quiz: Updating Documents in the shell.
Here is the question and answer.
Problem:
Given a pets collection where each document has the following structure and fields:
{
"_id": ObjectId("5ec414e5e722bb1f65a25451"),
"pet": "wolf",
"domestic?": false,
"diet": "carnivorous",
"climate": ["polar", "equatorial", "continental", "mountain"]
}
Which of the following commands will add new fields to the updated documents?
Answers are
db.pets.updateMany({ "pet": "cat" },
{ "$push": { "climate": "continental",
"look": "adorable" } })
and
db.pets.updateMany({ "pet": "cat" },
{ "$set": { "type": "dangerous",
"look": "adorable" }})
I wonder what would the document look like if I actually $set or $push like answers.
For $push
{
"_id": ObjectId("5ec414e5e722bb1f65a25451"),
"pet": "wolf",
"domestic?": false,
"diet": "carnivorous",
"climate": ["polar", "equatorial", "continental", "mountain"],
"look": "adorable" // added
}
For $set
{
"_id": ObjectId("5ec414e5e722bb1f65a25451"),
"pet": "wolf",
"domestic?": false,
"diet": "carnivorous",
"climate": ["polar", "equatorial", "continental", "mountain"],
"type": "dangerous", // added
"look": "adorable" // added
}
Would these be the outcome?
Also, does the first operator need to be {“pet”:“wolf”} for this case?
Thank you!
db.pets.updateMany({ "pet": "cat" }, // this
{....}