1

I need to find and update documents with category that corresponding to the query. Array could contain mo than one corresponding id.

Query:

{
 "ids": ["61f1cda47018c60012b3dd01", "61f1cdb87018c60012b3dd07"],
 "userId": "61eab3e57018c60012b3db3f"
}

I got collection with documents like:

`{

   "_id":{"$oid":"61f1cdd07018c60012b3dd09"},
   "expenses":[
     {"category":"61eafc104b88e154caa58616","price":"1111.00"},                  
     {"category":"61f1cdb87018c60012b3dd07","price":"2222.00"}, 
     {"category":"61f1cda47018c60012b3dd01","price":"1241.00"}, 
     {"category":"61f1cdb87018c60012b3dd07","price":"111.00"}
   ],
   "userId":"61eab3e57018c60012b3db3f"
 }`

my method:

  async myMethod(ids: [string], userId: string) {
try {
  const { ok } = await this.ExpensesModel.updateMany(
    {"userId": userId, "expenses.category": { $in: ids }},
    {$set: {"expenses.$.category": "newCategoryID"}}
  );
  return ok
} ........

I path array of ids ["61f1cda47018c60012b3dd01","61f1cdb87018c60012b3dd07","61f1cdb87018c60012b3dd07"] and userId, this code update only 1 category by document.

So can i made it with mongo build in methods? or i need to find matching document and update it it by my self and after that update or insert;

1 Answer 1

2

Update with arrayFilters

db.collection.update({
  "expenses.category": {
    $in: [
      "61f1cda47018c60012b3dd01",
      "61f1cdb87018c60012b3dd07"
    ]
  }
},
{
  $set: {
    "expenses.$[elem].category": "61eab3e57018c60012b3db3f"
  }
},
{
  arrayFilters: [
    {
      "elem.category": {
        $in: [
          "61f1cda47018c60012b3dd01",
          "61f1cdb87018c60012b3dd07"
        ]
      }
    }
  ]
})

mongoplayground

Sign up to request clarification or add additional context in comments.

4 Comments

your tip helped me move in the right direction, adapted it to my code, updated it a bit and everything worked, Thx a lot!
That's awesome! If you wanted to assign category to the value of userId, how could that be done? IOW, how could you replace the arrayFilters with something in a pipeline?
@rickhg12hs It would be better if you can edit your question with the exact needed. Make sure you want update the documents in db or find them out with aggregate.
Sorry, it's bad form for me to ask a question here in the comments since I'm not the OP, but I thought it might be a quick update to your mongoplayground.net contribution. Just so I can understand better MongoDB update, how could the above be modified so that rather than having a "constant" ("61eab3e57018c60012b3db3f") be the new value, instead have the new value be $userId. I tried just replacing the constant with $userId, but that didn't work, and arrayFilters don't seem to work with pipelines in update.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.