使用 Mongoose 查找和计数集合元素 [英] Find and Count elements of collection with Mongoose

查看:21
本文介绍了使用 Mongoose 查找和计数集合元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Mongoose 中,我需要查找集合中的元素并对其进行计数,并获取查找和计数的结果.我试过了

In Mongoose, I need to find elements in a collection and count them, and getting both the results of the find and count. I have tried

Model.find().count(function (err, count) {
    // Get count, but cannot get results of find
});

有没有办法在不调用两次的情况下同时获得 find() 和 count() ?

Is there a way to get both find() and count() without calling them twice?

推荐答案

可以使用返回数组的长度:

You can use the length of the returned array:

Model.find().exec(function (err, results) {
  var count = results.length

});

这篇关于使用 Mongoose 查找和计数集合元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆