简体   繁体   English

无法在mongodb collection.find中计数

[英]unable to get count in mongodb collection.find

I am using mongoose ODM for a application. 我在应用程序中使用猫鼬ODM。 I am trying to count the number of items returned by a model like this: 我试图计算这样的模型返回的项目数:

 app.post("/verifyLogin",function(request,response){
var usr=request.body.username;
var pass=request.body.password;

response.send(userModel.find({$and:[{username:"admin"},{password:"admin"}]}).count());

});

But i get in return: 但我得到回报:

   {
  "options": {
    "populate": {}
  },
  "_conditions": {
    "$and": [
      {
        "username": "admin"
      },
      {
        "password": "admin"
      }
    ]
  },
  "_updateArg": {},
  "op": "count"
}

I expected a number :( 我希望有一个号码:(

One way of doing this is to use Model.count method, in your case: 一种方法是在您的情况下使用Model.count方法:

userModel.count({$and:[{username:"admin"},{password:"admin"}]}, function(err, result) {
    if (err) {
       //handle errors
    } else {
       response.send({count :result});
    }
});

Also: you're in node.js world - use callbacks. 另外:您在node.js世界中-使用回调。

The result you're getting now is the result of the count call on the Model.find() result (which, i think, is a query object), not the count of the query results. 您现在得到的结果是对Model.find()结果(我认为是查询对象)的count调用的结果,而不是查询结果的计数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM