Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何统计mongodb集合中特定字段的实例_Node.js_Mongodb - Fatal编程技术网

Node.js 如何统计mongodb集合中特定字段的实例

Node.js 如何统计mongodb集合中特定字段的实例,node.js,mongodb,Node.js,Mongodb,这是我的mongodb中的收款 { studentIDnumber: "111" datepicker: "02/06/2019" transactType: "Graduation expenses" timepicker:"11:00am - 12:00pm" amount:"123" } { studentIDnumber: "111" datepicker: "02/03/2019" transactType: "Diploma" timepicker:"11:00am - 12:00

这是我的mongodb中的收款

{
studentIDnumber: "111"
datepicker: "02/06/2019"
transactType: "Graduation expenses"
timepicker:"11:00am - 12:00pm"
amount:"123"
}

{
studentIDnumber: "111"
datepicker: "02/03/2019"
transactType: "Diploma"
timepicker:"11:00am - 12:00pm"
amount:"123"
}

{
studentIDnumber: "111"
datepicker: "02/03/2019"
transactType: "Tuition fee"
timepicker:"11:00am - 12:00pm"
amount:"123"
}
我试图计算集合中的11:00-12:00pm数量,但它给了我一个未处理的PromisejectionWarning:TypeError:无法读取未定义的属性“count”,下面是我的nodejs代码。正确的方法是什么?有什么想法吗

const mongoose = require('mongoose')
const url = require('../config/keys').MongoURI

router.get('/dashboard', ensureAuthenticated, (req, res) =>{
  res.render('dashboard', {
    firstname: req.user.firstname,
    lastname : req.user.lastname
  }),

  mongoose.connect(url, {useNewUrlParser: true}, function(err, db){
    if (err) throw err
    db.payments.count({timepicker: "11:00am - 12:00pm"})

  })

});
使用此查询:

db.payments.find({timepicker: "11:00am - 12:00pm"}).count(function(err, count) 
    {
        if (err) throw err;
        console.log(count);

    });
如果你使用猫鼬,那么

 PaymentModel.count({timepicker: "11:00am - 12:00pm"}, function(err, c) {
       console.log('Count is ' + c);
  });

您不需要每次都连接到数据库。一旦连接,mongoose构造器将其添加为全局。可能的重复工作!但我只是将计数替换为countDocuments,因为我收到一条警告,它将被弃用。