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?
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?
试试$exists
运营商:
$this->mongolib->update('people', array('age' => array('$exists' => false)), array('$set' => array('age' => 14)), array('upsert' => true, 'multiple' => true));
我有一个我能想到的迂回方式。我建议您使用正常的更新命令,但只需在您的选择标准中添加一个正则表达式条件,以仅匹配那些没有为“年龄”属性设置值(或数值)的文档。