M001: Quiz: Updating Documents in the shell

Concerns On Quiz: Updating Documents in the shell

All options are wrong. May need correction “wolf” instead of “cat”. either way question and answer that mongo is pushing ae wrong

Test 01:

Atlas atlas-jdjvll-shard-0 [primary] sample_training> db.pets.find();
[
  {
    _id: ObjectId("623764be39ca35e950c69a38"),
    pet: 'wolf',
    'domestic?': false,
    diet: 'carnivorous',
    climate: [ 'polar', 'equatorial', 'continental', 'mountain' ]
  }
]
Atlas atlas-jdjvll-shard-0 [primary] sample_training> db.pets.updateMany({ "pet": "cat" },
...                    { "$push": { "climate": "continental",
.....                                 "look": "adorable" } })
{
  acknowledged: true,
  insertedId: null,
  matchedCount: 0,
  modifiedCount: 0,
  upsertedCount: 0
}
Atlas atlas-jdjvll-shard-0 [primary] sample_training>

Atlas atlas-jdjvll-shard-0 [primary] sample_training> db.pets.find();
[
  {
    _id: ObjectId("623764be39ca35e950c69a38"),
    pet: 'wolf',
    'domestic?': false,
    diet: 'carnivorous',
    climate: [ 'polar', 'equatorial', 'continental', 'mountain' ]
  }
]
Atlas atlas-jdjvll-shard-0 [primary] sample_training> db.pets.updateMany({ "pet": "cat" },
...                    {"$set": { "domestic?": true, "diet": "mice" }})
{
  acknowledged: true,
  insertedId: null,
  matchedCount: 0,
  modifiedCount: 0,
  upsertedCount: 0
}
Atlas atlas-jdjvll-shard-0 [primary] sample_training> db.pets.updateMany({ "pet": "cat" },
...                    { "$set": { "climate": "continental" }})
{
  acknowledged: true,
  insertedId: null,
  matchedCount: 0,
  modifiedCount: 0,
  upsertedCount: 0
}
Atlas atlas-jdjvll-shard-0 [primary] sample_training>

Test 02:

Atlas atlas-jdjvll-shard-0 [primary] sample_training> db.pets.updateMany({ "pet": "wolf" },
...                    { "$set": { "type": "dangerous",
.....                                "look": "adorable" }})
{
  acknowledged: true,
  insertedId: null,
  matchedCount: 1,
  modifiedCount: 1,
  upsertedCount: 0
}
Atlas atlas-jdjvll-shard-0 [primary] sample_training> db.pets.updateMany({ "pet": "wolf" },
...                    { "$push": { "climate": "continental",
.....                                 "look": "adorable" } })
MongoServerError: The field 'look' must be an array but is of type string in document {_id: ObjectId('623764be39ca35e950c69a38')}
Atlas atlas-jdjvll-shard-0 [primary] sample_training>

Atlas atlas-jdjvll-shard-0 [primary] sample_training> db.pets.updateMany({ "pet": "wolf" },
...                    {"$set": { "domestic?": true, "diet": "mice" }})
{
  acknowledged: true,
  insertedId: null,
  matchedCount: 1,
  modifiedCount: 1,
  upsertedCount: 0
}
Atlas atlas-jdjvll-shard-0 [primary] sample_training> db.pets.updateMany({ "pet": "wolf" },
...                    { "$set": { "climate": "continental" }})
{
  acknowledged: true,
  insertedId: null,
  matchedCount: 1,
  modifiedCount: 1,
  upsertedCount: 0
}
Atlas atlas-jdjvll-shard-0 [primary] sample_training>

Atlas atlas-jdjvll-shard-0 [primary] sample_training> db.pets.find();
[
  {
    _id: ObjectId("623764be39ca35e950c69a38"),
    pet: 'wolf',
    'domestic?': true,
    diet: 'mice',
    climate: 'continental',
    look: 'adorable',
    type: 'dangerous'
  }
]
Atlas atlas-jdjvll-shard-0 [primary] sample_training>

See Chapter 4: Advanced CRUD Operations- Lab: $expr - #2 by steevej
and Creating and Manipulating Documents - #2 by steevej