IGJ
IGJ

Reputation: 95

How do I update the messageRead attribute in my MongoDb object?

I have the following object stored in MongoDb. I am sending a messageRead attribute inside my messages array.

enter image description here

I have tried:

 collection.updateOne({ '_id': ObjectId(employeeID) }, 

                      { 
                        
                        "$set": { 
                  
                          "userObject.messages.message.message_uuid" : {  employeeMessageUpdateUUID, "messageRead" : employeeMessageRead } 
                      
                      
                      
                      }

but it does not work. I find the object i'm looking for through the _id, and then try to find the message using the message_uuid however the messageRead attribute is not updating. I am clearly using the wrong Mongo query.. What should my $set look like?

Upvotes: 1

Views: 34

Answers (1)

Cuong Le Ngoc
Cuong Le Ngoc

Reputation: 11975

You can use $ operator to do that:

collection.updateOne(
  { 
    '_id': ObjectId(employeeID),
    'userObject.messages.message.message_uuid': employeeMessageUpdateUUID
  }, 
  {
    $set: { 'userObject.messages.$.message.messageRead': employeeMessageRead }
  }
)...

Upvotes: 1

Related Questions