downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | conferences | my php.net

search for in the

問い合わせ> <持続的接続 (バージョン 1.1.4 まで限定)
[edit] Last updated: Fri, 17 May 2013

view this page in

書き込み

ネストしたオブジェクトの更新

次のドキュメントで、コメントの author を変更することを考えましょう。

{ 
    "_id" : ObjectId("4b06c282edb87a281e09dad9"), 
    "content" : "this is a blog post.",
    "comments" : 
    [
        {
            "author" : "Mike",
            "comment" : "I think that blah blah blah...",
        },
        {
            "author" : "John",
            "comment" : "I disagree."
        }
    ]
}
内部のフィールドを変更するには、$set (他のフィールドが削除されてしまわないように!) を使って変更したいコメントのインデックスを指定します。
<?php

$blog
->update($criteria, array('$set' => array("comments.1" => array("author" => "Jim"))));

?>

位置指定演算子

位置指定演算子 $ は、配列内のオブジェクトを更新するときに有用です。 たとえば上の例で、実際に変更したいコメントのインデックスがわからないけれども "John" を "Jim" に変更しなければならないという状況を考えてみましょう。 そんなときには $ が使えます。

<?php

$blog
->update(
    array(
"comments.author" => "John"), 
    array(
'$set' => array('comments.$.author' => "Jim")));

?>


add a note add a note User Contributed Notes 書き込み - [0 notes]
There are no user contributed notes for this page.

 
show source | credits | sitemap | contact | advertising | mirror sites