0

I have a field which stores an array of objects. I want to modify the object's "status" value depending on the "id" value the object possesses.

"authors" : [ 
        {
            "id" : "18",
            "first_name" : "Buddhika",
            "last_name" : "Chathuranga",
            "$$hashKey" : "object:35",
            "status" : NumberLong(0)
        }, 
        {
            "id" : "3", // search for this number
            "first_name" : "Pasindu",
            "last_name" : "Priyanath",
            "$$hashKey" : "object:43",
            "status" : NumberLong(0) // modify this to 1
        }
    ],

2 Answers 2

2

We can leverage positional update in MongoDB to update values inside array.

Mongo Positional Update

Please find script below.

db.authors.update(
{"authors.id": "3"},
{ $set: { "authors.$.status" : NumberLong(1) }} 
)
Sign up to request clarification or add additional context in comments.

Comments

0

Update in Mongo DB php:

db.collection.update( criteria, objNew, upsert, multi );

Detail Explanation

Comments

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.