如何在节点js中查找结果计数

时间:2016-09-19 13:19:15

标签: node.js mongodb express

我试图查找结果的计数,但我不确定如何使用它。任何人都可以建议帮助

  exports.shownotifications = function (req, res) {
  var params = req.params;console.log(params)
    var record= db.collection('requests');
    var item = {"sent_id": params.id,"status":1};console.log(item)
  record.find(item).toArray((err, result) => {
   if (err){ return console.log(err)
    }
      if(result){
            response = {status:'success',data:result};console.log(response)
        } else{
            response = {status:'fail',data:[]};
        }
      res.send(response);
  });

};

1 个答案:

答案 0 :(得分:0)

您可以使用db.collection.count()

  exports.shownotifications = function(req, res) {
    var params = req.params;
    var record = db.collection('requests');
    var item = {
        "sent_id": params.id,
        "status": 1
    };
    record.count(item), (err, result) => {
        if (err) {
        }
        if (result) {
            response = {
                status: 'success',
                data: result
            };
        } else {
            response = {
                status: 'fail',
                data: []
            };
        }
        res.send(response);
    }
  };