We need to be able to use $[<identifier>] and "$setOnInsert" in the same command
I want to be able to have a maintain array of counters for a user through a single update statement. If the document containing array of counters does not exist, I want to add it. If it does exist, I want to increment the counter
For example, this command
db.inboxItemCounts.updateOne(
// filter
{
"userId": userDoc.userId
},
// update
{
"$setOnInsert": {
"userId": userDoc.userId,
"fromUserSummary": [{
"userName": fromUserDoc.userName,
"count": 1
}]
},
// "$inc": incBody,
"$inc": {
"fromUserSummary.$[userElement].count": 1
}
},
// options
{
"upsert": true,
"writeConcern": { "w": "majority" },
"arrayFilters": [
{ "userElement.userName": { $eq: fromUserDoc.userName }}
]
}
);
produces this error
"MongoServerError: Updating the path 'fromUserSummary.$[userElement].count' would create a conflict at 'fromUserSummary"
That's ridiculous because the command knows that what's is to be used on insert versus update.