次のドキュメントで、コメントの 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."
}
]
}
<?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')));
?>