There are a few scenarios where both modifiedCount
and upsertedCount
can be 0:
- When a filter within the update operation does not match:
db.collection.updateOne(
{_id: 2},
{ $set: { items.$[elem]: 4 } },
{ arrayFilters: [{elem: 3}], upsert: true }
)
In this example, the filter {elem: 3}
will not match any element in the items
array. As a result, the document will be found (setting matchedCount
to 1), but no modifications will be made (setting both modifiedCount
and upsertedCount
to 0).
$setOnInsert
:db.collection.updateOne(
{ name: "David" },
{ $setOnInsert: { firstSeenAt: new Date() } },
{ upsert: true }
)
In this example, if the document with name: "David"
already exists, the update operation will not modify the document (setting modifiedCount
to 0). However, since upsert
is set to true
, the firstSeenAt
field will be set if the document does not exist, resulting in upsertedCount
being 0 as well.