开发者

Using multiple modifier operations in a single update in PHP with MongoDB?

开发者 https://www.devze.com 2023-01-31 04:41 出处:网络
Is it possible to use multipule modifier operations within a single update in PHP with MongoDB? For example, if I created the following document:

Is it possible to use multipule modifier operations within a single update in PHP with MongoDB?

For example, if I created the following document:

$doc = array(
'one' => 1,
'tags' => array(),
);

And then wanted to add another field ('new') and add an element to the 'tags' array, I could use the following two lines:

$collection->updat开发者_运维技巧e(array('_id'=>$doc['_id']), array('$set' => array('new'=>'value')));

and

$collection->update(array("_id" => $doc['_id']), array('$addToSet' => array('tags'=>'my tag')));

Does anyone know it's possible to do both of these two operations in one 'update' command?

Thanks, Neil


I haven't used mongo in a while, but I believe you should be able to:

$collection->update(array('_id'=>$doc['_id']), array(
    '$set' => array('new'=>'value'),
    '$addToSet' => array('tags'=>'my tag')
));
0

精彩评论

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

关注公众号