Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mongodb 如果不添加字段,是否可以检查流星集合中是否存在字段?_Mongodb_Meteor - Fatal编程技术网

Mongodb 如果不添加字段,是否可以检查流星集合中是否存在字段?

Mongodb 如果不添加字段,是否可以检查流星集合中是否存在字段?,mongodb,meteor,Mongodb,Meteor,在Meteor Mongo集合中,我们是否可以检查字段是否存在,如果不存在,则添加该字段?是。您需要在Mongo查询中使用$exists操作符。例如,对于名为Posts的集合: Posts.update({ _id: 'abcdef123', myField: { $exists: false } }, { $set: { myField: 'myValue...' } }); 如果myField已存在,则不会更新该字段

在Meteor Mongo集合中,我们是否可以检查字段是否存在,如果不存在,则添加该字段?

是。您需要在Mongo查询中使用
$exists
操作符。例如,对于名为
Posts
的集合:

Posts.update({
    _id: 'abcdef123',
    myField: {
        $exists: false
    }
}, {
    $set: {
        myField: 'myValue...'
    }
});
如果
myField
已存在,则不会更新该字段


这里需要注意的是,如果您想更新其他字段,而不管是否存在
myField
,则需要在单独的更新查询中进行更新。

是。您需要在Mongo查询中使用
$exists
操作符。例如,对于名为
Posts
的集合:

Posts.update({
    _id: 'abcdef123',
    myField: {
        $exists: false
    }
}, {
    $set: {
        myField: 'myValue...'
    }
});
如果
myField
已存在,则不会更新该字段

这里需要注意的是,如果您想更新其他字段,而不管是否存在
myField
,则需要在单独的更新查询中进行更新。

可能重复的