MongoDB - PHP 修改数组的对象值

MongoDB - PHP modifying array's object values

我有一个存储对象数组的字段。我想根据对象拥有的 "id" 值修改对象的 "status" 值。

"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
        }
    ],

在 Mongo 数据库中更新 php:

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

Detail Explanation

我们可以利用 MongoDB 中的位置更新来更新数组中的值。

Mongo Positional Update

请在下面找到脚本。

db.authors.update(
{"authors.id": "3"},
{ $set: { "authors.$.status" : NumberLong(1) }} 
)