如果文档存在,则流星返回true / false

时间:2015-08-04 15:16:07

标签: meteor

如何确定Meteor中集合中是否存在文档?

编辑:新代码。

mongodb有一个带有ProductName:Apples的文档 输入产品是“苹果”

var exists = Products.find({ProductName: inputproduct});
                    if(exists)
                    {
                        alert("it exists");
                    }else{
                        alert('doesnt exist');
                    }

所有我回来的是:“它存在”,无论inputproduct的价值如何。我输出了什么输入产品,它回来“苹果”没问题。不知道这里发生了什么。使用find或findOne尝试了几种方法。

1 个答案:

答案 0 :(得分:3)

你几乎拥有它。但是,如果找不到匹配项,meteor的collection.findOne将返回匹配的第一个对象,或者未定义的(对于falsy)。试试这个:

var exists = Products.findOne(selector, projection);
if(exists)
 {
    do something...
 }