5

I have a document like this (this is very simplified):

{
    'name' : 'test',
    'age' : 16
}

and also this

{
    'name' : 'test2'
}

I want to set all 'age' to 14 if it does not exist.

How can I do this?

4

2 回答 2

6

试试$exists 运营商

$this->mongolib->update('people', array('age' => array('$exists' => false)), array('$set' => array('age' => 14)), array('upsert' => true, 'multiple' => true));
于 2012-07-19T05:51:32.987 回答
0

我有一个我能想到的迂回方式。我建议您使用正常的更新命令,但只需在您的选择标准中添加一个正则表达式条件,以仅匹配那些没有为“年龄”属性设置值(或数值)的文档。

于 2012-07-19T05:48:59.987 回答