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

问题描述

在猫鼬中,我需要在集合中查找元素并对其进行计数,并同时获得查找和计数的结果.我尝试过

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

});

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

09-05 18:08
查看更多