Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
735 views
in Technique[技术] by (71.8m points)

node.js - Find and Count elements of collection with 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
});

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use the length of the returned array:

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

});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...