0

我的数据是这样的:

Array
(
    [_id] => MongoId Object
        (
            [$id] => 51a6fca3f348aca011000000
        )

    [first_name] => Phan
    [last_name] => Chuong
    [interests] => Array
        (
            [0] => football
            [1] => swimming
            [2] => PHP
            [3] => music
        )

)

我想将值PHP编辑为PHP1或将PHP设置为null。请帮帮我我该怎么做?谢谢大家!

4

1 回答 1

2

您可以像这样使用 MongoCollection::update() 方法:

// $c is your MongoCollection Object

$updatedata = array('$set' => array("interests.2" => "PHP1"));
$id = new MongoID('51a6fca3f348aca011000000')
$c->update(array("_id" => $id), $updatedata);

update 方法需要两个数组,第一个将告诉 DB 要更新哪个 Object(在本例中,id = 我们的 id,总是使用 MongoID 对象,总是!),第二个数组定义要更新的内容,注意你可以使用上面的点获取嵌套数组中的值。$set 是一个字段更新运算符,请在此处阅读更多信息:http: //docs.mongodb.org/manual/reference/operator/update-field/

或者您可以获取整个数组,对其进行更改,然后将其保存回来。

$cursor = $c findOne("_id",4cb30f560107ae9813000000);
$cursor['interests'][2] = 'whatever';
$c->update($cursor);
于 2013-05-30T11:18:02.377 回答