What you described is an update of the array, not a deletion of field. That’s why it appears in the updates and not in the deletes.
Let me explain with an example. First I insert a document:
> db.coll.insertOne({firstname:"Maxime",surname:"Beugnet", pets: [{name: "Bob"},{name: "Raoul"}]})
{
"acknowledged" : true,
"insertedId" : ObjectId("60a52dc14328bfa256cd18ba")
}
Then I remove one of my pet from the pets
array by updating it with a $pop
operation:
> db.coll.updateOne({}, {$pop: {pets: 1}})
Which results in this change event:
{
"_id" : {
"_data" : "8260A52E05000000012B022C0100296E5A10041EA1C19171774980BB0020FFA9BD24F746645F6964006460A52DC14328BFA256CD18BA0004"
},
"operationType" : "update",
"clusterTime" : Timestamp(1621437957, 1),
"ns" : {
"db" : "test",
"coll" : "coll"
},
"documentKey" : {
"_id" : ObjectId("60a52dc14328bfa256cd18ba")
},
"updateDescription" : {
"updatedFields" : {
"pets" : [
{
"name" : "Bob"
}
]
},
"removedFields" : [ ]
}
}
As expected, the pets
field is the one that has been updated. One of its value (here a subdocument) has been removed.
If I $unset
one of my fields though:
> db.coll.updateOne({}, {$unset: {surname: 1}})
I do get this change event that contains, this time, a field deletion:
{
"_id" : {
"_data" : "8260A52E3A000000012B022C0100296E5A10041EA1C19171774980BB0020FFA9BD24F746645F6964006460A52DC14328BFA256CD18BA0004"
},
"operationType" : "update",
"clusterTime" : Timestamp(1621438010, 1),
"ns" : {
"db" : "test",
"coll" : "coll"
},
"documentKey" : {
"_id" : ObjectId("60a52dc14328bfa256cd18ba")
},
"updateDescription" : {
"updatedFields" : {
},
"removedFields" : [
"surname"
]
}
}
This works as designed.
I hope this helps. Sorry if it’s not what you expected. There is currently no work around to “fix” this to my knowledge, but stay tuned for MongoDB 5.0…
Cheers,
Maxime.