0

Supposedly I want to update the object values of array of a document, but first I will need to look for its id first, heres the structure of document

{
_id: ObjectId('12312')
notifications: [
       {
         _id: ObjectId('1')
          status: 'unread'
       },
       {
         _id: ObjectId('2')
          status: 'unread'
       },
       {
         _id: ObjectId('3')
          status: 'unread'
       }
    ]
}


I want to update the status of notifications under that user where the objectId must be equal to inside the result array

result = 
  [
  new ObjectId("1"),
  new ObjectId("2"),
  ]

this should yield this result after the update

{
_id: ObjectId('12312')
notifications: [
       {
         _id: ObjectId('1')
          status: 'read'
       },
       {
         _id: ObjectId('2')
          status: 'read'
       },
       {
         _id: ObjectId('3')
          status: 'unread'
       }
    ]
}

Heres what Ive done so far but Im not getting the result i need

  const updateuser = await User.updateMany({_id:idofuser, "notifications._id": { $in : result }},{
     $set: {
            "notifications.$.status": "read",
         }
   })
1
  • ObjectId('1') of you is an object, you need a function that compares objects before merging Commented Jun 17, 2022 at 1:32

1 Answer 1

2

You can use $map to iterate through notifications and use $cond and $mergeObjects to perform the conditional update.

db.collection.update({
  "_id": "12312"
},
[
  {
    "$set": {
      "notifications": {
        "$map": {
          "input": "$notifications",
          "as": "n",
          "in": {
            "$cond": {
              "if": {
                "$in": [
                  "$$n._id",
                  [
                    "1",
                    "2"
                  ]
                ]
              },
              "then": {
                "$mergeObjects": [
                  "$$n",
                  {
                    "status": "read"
                  }
                ]
              },
              "else": "$$n"
            }
          }
        }
      }
    }
  }
],
{
  multi: true
})

Here is the Mongo Playground for your reference.

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

3 Comments

Hello sir thankyou for that answer, but may I ask, what is the purpose of multi property in the aggregate
@JASMAL actually not really useful for your case. It is just there for demonstrating a more general usage. You can check out its usage through this official document
@JASMAL I guess I can elaborate more. Your criteria specify by _id. As _id must be unique, there will be at most 1 document being matched. The multi option is hence not required as it is only required when multiple documents are required to be updated.

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.