开发者

MongoDB update replaces document

I'm trying to update a MongoDB document via PHP. My problem is that the documents data are removed when i update, thus other data than what I update is gone.

Before I update, the document looks like this:

{
    "_id": {
        "$oid": "4e178b45419866350f000001"
    },
    "Twitter": {
        "_id": {
            "$oid": "4e178b45419866350f000000"
        },
        "created": {
            "$date": "2011-07-08T22:57:09Z"
        },
        "userid": 开发者_如何学编程"5552362"
    },
    "created": {
        "$date": "2011-07-08T22:57:09Z"
    }
}

Here's how I update:

    $r = $Profile->update(
        array('Twitter._id' => new MongoId($profile['_id'])),
        array(
            '$set' => array(
                'Twitter' => array(
                    'name' => $user['name'],
                    'username' => $user['screen_name'],
                    'url' => $user['url'],
                    'modified' => new MongoDate()
                )
            )
        ),
        array('safe' => true)
    );
    debug($r); 


I think you want this kind of update:

$Profile->update(
    array('Twitter.userid' => $user['id']),
    array(
        '$set' => array(
            'Twitter.name' => $user['name'],
            'Twitter.username' => $user['screen_name'],
            'Twitter.url' => $user['url'],
            'Twitter.modified' => new MongoDate()
        )
    ),
    array('multiple' => true)
);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜