Mongo嵌套对象数组更新


Mongo nested object array update

我正在尝试更新文档中的值,但我还没有看到一个示例向我展示我需要做什么

{
    "_id" : ObjectId("5429e8a53150d03d541c7a53"),
    "foo" : {
        "bar" : {
            "0" : {
                "email" : {
                    "0" : "bob@aol.com"
                }
            }
        }
    }
}

这当然不是我真正的对象,但代表了结构。如何更新bob@aol.com将来whatever@whocares.net?

此外,我将如何删除相同的元素?

另外(对于bunus点)我将如何使用phpMongo类更新它?

好的,我想明白了。这就是你在控制台中的操作方式:

db.test.update({"foo.bar.0.email" : {"0":"bob@aol.com"} },{$set:{"foo.bar.0.email.0":"whatever@whocares.net"}});

这就是在PHP中使用Mongo类的方法

$db = new Mongo("connection stuff");
$condition = array('foo.bar.0.email' => array('0' => 'bob@aol.com'));
$data = array('$set' => array('foo.bar.0.email.0' => 'whatever@whocares.net'));
$db->test->update($condition, $data);

我希望这能帮助到别人,因为我花了几个小时才弄清楚。