我想:
- fetch some documents
- for every document, set a field:
- change it, if it exists
- add it, if it doesn t
我做的是:
// fresh data
print_r(iterator_to_array($collection->find()->limit(2)));
// query
$docs = $collection->find()->limit(2);
// fetch
foreach ( $docs AS $id => $doc ) {
// update
$collection->update(array( _id => $doc[ _id ]), array(
$set => array(
existing_field => x ,
new_field => y ,
),
), array( multiple => false));
}
// verify
print_r(iterator_to_array($collection->find()->limit(2)));
为什么不做什么? 现有领域没有变化,新领域也增加了。 条件可能是错误的吗?
PS. The full code (with a lot of junk in between): http://pastebin.com/CNfCVxex
lines 33 - 37 -- fresh data
lines 63 - 76 -- query, fetch & update
lines 79 - 80 -- verify