I have the following MongoDB shell script which is using bulkWrite to update a single document.
Here's the catch: it's updating an array field. It works! As in, if I manually check the document before and then after, the item has been removed from the array field.
But a bulkWrite returns a specific result object. And this is -failing- to report that anything got updated. But the document -was- updated!
Is there something specific I need to check to see that this has worked?
MongoDB: v4.2
Document
{
"_id" : {
"userId" : <some guid>
},
"name" : "han solo",
"teams" : [
{
"isMainTeam": false,
"team" : {
"_id" : {
"teamId": <some guid>
},
"name" : "red team"
}
},
{
"isMainTeam" : true,
"team" : {
"_id" : {
"teamId": <some guid>
},
"name" : "blue team"
}
}
]
}
Javascript query
let operations = [];
const updateOperation = {
updateOne: {
"filter" : { '_id.userId': user.userId },
"update" : {
$pull: {
'teams' : {
'team._id.teamId' : {
$in: user.teams
}
}
}
}
}
};
operations.push(updateOperation); // Image this has 10 operations. not just 1.
const result = bulkWrite(operations);
Result from bulkWrite -> aka printjson(result);
{
"acknowledged" : true,
"deletedCount" : 0,
"insertedCount" : 0,
"matchedCount" : 10,
"upsertedCount" : 0,
"insertedIds" : {
},
"upsertedIds" : {
}
}
And I can 100% confirm that the array had 4 items in it, and then 2 items were correctly removed. Leaving 2 items .. for that example user, above.
What am I missing here?
EDIT: I just found another post from today which looks like it describing the exact same thing! REF: Why is modifiedCount missing in Mongo bulkWrite() result?