简体   繁体   English

使用节点js +和尚+ ajax获取mongodb中集合的记录总数

[英]Getting the total number of records of a collection in mongodb using node js + monk + ajax

Total noob here, i only know how to do CRUD in Mongodb using node js+monk+ajax but I don't have any idea on how to get the total number of records in my collection. 总的来说,我只知道如何使用node js + monk + ajax在Mongodb中进行CRUD,但我对如何获取集合中的记录总数一无所知。

This is what i have tried so far and it is returning undefined value: 这是我到目前为止尝试过的,并且返回未定义的值:

router.get('/getTotalrecord'', function (req, res) { 
    var db = req.db; 
    var collection = db.get('department'); 
    res.send(collection.count); 
});

Did you look at count ? 你看count吗? It counts how many documents answer a given query: 它计算有多少文档回答给定查询:

collection.count({})
    .then(count => res.send(count));

you should use this function 你应该使用这个功能

exports.getCount = (req, res, next) => {
  Users.find({}, { __v: 0 })
    .then(users =>
      res.status(200).json({
        status: true,
        error_num: '',
        result: users.length,
        error: ''
      })
    )
    .catch(err => {
      next(err);
    });
};

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

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