Meteor:查询字段中是否存在字符串值并返回布尔响应

时间:2015-03-09 14:45:11

标签: mongodb meteor

如何检查mongo集合中字段中是否存在特定字符串值?

标准collection.find会返回一个游标,但我试图得到一个真或假的回复。

所以我正在做以下不正确的事情。

var booleanResponse = collection.find({}, {'field': 'valueToCheck'})
console.log(booleanResponse)

我也尝试过以下操作,但操作会再次检查一个数组,但我的字段包含一个字符串...

var booleanResponse = collection.find({},
    { field: { $exists: true, $nin: [valueToCheck]} })

2 个答案:

答案 0 :(得分:2)

您可以像这样查看count

var fancyPostsExist = Posts.find({type: 'fancy'}, {limit: 1}).count() > 0;

在此示例中,如果fancyPostsExist的字段true等于Posts,则type将为'fancy'false否则。

答案 1 :(得分:1)

如果您想检查特定集合中的字段字段的值是否为,您可以执行以下操作:

var hasValue = YourCollection.findOne({field: 'value'}) === undefined ? false : true