Skip to Main Content

MongoByte MongoDB Logo

Welcome to the new MongoDB Feedback Portal!

{Improvement: "Your idea"}
We’ve upgraded our system to better capture and act on your feedback.
Your feedback is meaningful and helps us build better products.

Status Submitted
Categories Database
Created by Guest
Created on Sep 22, 2022

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.
  • Attach files