What would a document look like? when you use $set or $put

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 
{....}

You could try it to see. This is one of the beauty of the flexible schema nature of MongoDB. Just insert the first document in a collection. And perform the update.

Yes to update the specific document. You can use pet:wolf when you try so that you match the only document in the collection. But the question indicates document with the given structure. So the query part is not important compared to the update part.

Always refer to documentation for the exact behaviour.

For $push you will read

The $push operator appends a specified value to an array.

and

If the field is absent in the document to update, $push adds the array field with the value as its element.

So for

Your questioning should be:

  1. Does climate field exists and is it an array
  2. It does, ok and continental already exists in the array, does the documentation indicates that $push make sure that there is not duplicate
  3. Does look field exists and is it an array
  4. It does not. Is look:adorable an array field with a single value or is look:adorable a simple string field?

The fields type and look for your $set look ok.

1 Like

@steevej

Thank you very much for your clear explanation!
So $push is for when you add values to the array.

These below are for who need to see example.

Outcome for $push should be like

[
  {
    _id: ObjectId("62729401f08d78e7637d3ac8"),
    pet: 'wolf',
    'domestic?': false,
    diet: 'carnivorous',
    climate: [ 'polar', 'equatorial', 'continental', 'mountain', 'continental' ], // added
    look: [ 'adorable' ] // added
  }
]

$push is for when you add value to the array, if you use $set to add new value to the array, the whole array value replace to value you set.

For example,

db.practice.updateOne({ "pet": "cat" },
                   { "$set": { "climate": "continental", 
                               "look": "adorable" }})

outcome for this

[
  {
    _id: ObjectId("6272951cf08d78e7637d3ac9"),
    pet: 'cat',
    'domestic?': false,
    diet: 'carnivorous',
    climate: 'continental', // replaced the whole array
    look: 'adorable',
    type: 'dangerous'
  }
]

The whole climate field replaced because of $set.

1 Like