PHP MongoDB更新没有';行不通

PHP MongoDB更新没有';行不通,php,mongodb,Php,Mongodb,测试数组如下所示。我想在列表检查id中添加“类别”字段和值“0” 我试过了 { "__v" : 2, "_id" : ObjectId("54dc4fd10ac1f6a066a0646c"), "desc" : "test1", "lists" : [ { "index" : 1, "text" : "__point", "_id" : ObjectId("54dc4fda0

测试数组如下所示。我想在列表检查id中添加“类别”字段和值“0”

我试过了

{
    "__v" : 2,
    "_id" : ObjectId("54dc4fd10ac1f6a066a0646c"),
    "desc" : "test1",
    "lists" : [ 
        {
            "index" : 1,
            "text" : "__point",
            "_id" : ObjectId("54dc4fda0ac1f6a066a0646d"),
            "createdAt" : ISODate("2015-02-12T07:01:46.390Z")
        }, 
        {
            "index" : 2,
            "text" : "__point",
            "_id" : ObjectId("54dc4fdd0ac1f6a066a0646e"),
            "createdAt" : ISODate("2015-02-12T07:01:49.668Z")
        }
    ],
    "name" : "test" }

像这样,但它不起作用。

嗨,我不知道有关PHP的更多信息,但您可以通过下面的脚本来完成

$collection->update(array('_id' => new MongoId("54dc4fd10ac1f6a066a0646c")),array('$set' => array(new MongoId("54dc4fda0ac1f6a066a0646d").".lists.category" => '0')));
db.collectionName.find({
“名单”:{
“$exists”:true
}
}).forEach(函数(数据){
对于(vari=0;这可能会对您有所帮助。Scott
    db.collectionName.find({
    "lists": {
    "$exists": true
    }
}).forEach(function(data){
    for(vari=0;i<data.lists.length;i++){
    db.collectionName.update({
        "_id": data._id,
        "lists.index": data.lists[
            i
        ].index
    },
    {
        "$set": {
            "lists.$.category": 0
        }
    },
    true,
    true);
    }
})
set third parameter of update to true.
i think there is some problem in finding which embedded document for updated.
try this one might be work.
$collection->update(array('_id' => new MongoId("54dc4fd10ac1f6a066a0646c"), 'lists._id' => "54dc4fda0ac1f6a066a0646d"), array('$set' => array("lists.$.category" => '0')), true);