检查集合是否包含至少一个文档

时间:2015-11-07 12:28:49

标签: javascript mongodb meteor

我目前正在尝试编程登录Meteor(是的,我知道默认的登录包)。用户按下登录表单上的提交,完成了流星方法调用:

Template.Login.events({
"submit form": function(event, doc){
    event.preventDefault();
    var username = doc.find("#login-username").value;
    var password = doc.find("#login-password").value;
    Meteor.call("isAdmin",username,password, function(error, result){
        console.log("Catchback from Method Call: ");
        console.log(error);
        console.log(result);
    });
}
});

Meteor方法,用于检查集合是否包含条目:

Meteor.methods({
  isAdmin: function (username, password){
      return !!Admins.find({username: username, password: password});   
  }
});

现在的问题是,当集合包含此用户的条目时,我只能找到一种方法来返回true,否则就是false。它目前只对一切都返回true。

1 个答案:

答案 0 :(得分:1)

collection.find([selector], [options])函数返回一个游标。因此,您可以使用collection.find([selector], [options]).count(),它返回与h2{ display: inline-block; border-bottom: 3px solid #ffcc00; padding-bottom: 30px; color: #5e5e5e; font-size: 23px; font-weight: 400; line-height: 18px; text-align: left; margin: 30px 3px; position:relative; /*add this*/ } h2:after{ content:''; display:inline-block; position:absolute; width:80%; background:#e6e6e6; height:3px; top:100%; } 查询匹配的文档计数。因此,您可以检查此计数值是否大于零。

在你的情况下:

find()
相关问题