在这里,我只知道如何使用节点js+monk+ajax在Mongodb中执行CRUD,但是我不知道如何在我的集合中获得总记录数。
这是我到目前为止尝试过的,它返回了未定义的值:
router.get('/getTotalrecord'', function (req, res) {
var db = req.db;
var collection = db.get('department');
res.send(collection.count);
});
发布于 2018-03-18 14:27:37
您应该使用此函数。
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);
});
};
发布于 2016-12-15 07:36:11
你看过count
吗?它计算出有多少文档回答了给定的查询:
collection.count({})
.then(count => res.send(count));
https://stackoverflow.com/questions/41143463
复制相似问题