How to get the whole mongoose model score?

How to find out the number of models that were saved in memory? there is a Model.count() method, but it does not work.

 var db = mongoose.connect('mongodb://localhost/myApp'); var userSchema = new Schema({name:String,password:String}); userModel =db.model('UserList',userSchema); var userCount = userModel.count('name'); 

userCount an object that the called method can get a real count ?

thank

+86
mongodb mongoose
May 30 '12 at 7:33
source share
6 answers

The code below works. Pay attention to the use of countDocuments .

  var mongoose = require('mongoose'); var db = mongoose.connect('mongodb://localhost/myApp'); var userSchema = new mongoose.Schema({name:String,password:String}); var userModel =db.model('userlists',userSchema); var anand = new userModel({ name: 'anand', password: 'abcd'}); anand.save(function (err, docs) { if (err) { console.log('Error'); } else { userModel.countDocuments({name: 'anand'}, function(err, c) { console.log('Count is ' + c); }); } }); 
+108
May 30 '12 at 10:22
source share

The reason your code does not work is because the count function is asynchronous, it does not return the value synchronously.

Here is a usage example:

 userModel.count({}, function( err, count){ console.log( "Number of users:", count ); }) 
+135
Mar 26 '13 at 12:14
source share

Collection.count is deprecated and will be removed in a future version. Use a collection instead. countDocuments or collection. timateDocumentCount .

 userModel.countDocuments(query).exec((err, count) => { if (err) { res.send(err); return; } res.json({ count: count }); }); 
+21
Aug 18 '18 at 0:55
source share

You must give the object as an argument

 userModel.count({name: "sam"}); 

or

 userModel.count({name: "sam"}).exec(); //if you are using promise 

or

 userModel.count({}); // if you want to get all counts irrespective of the fields 

The latest version of mongoose count () is deprecated, so use

 userModel.countDocuments({name: "sam"}); 
+19
Apr 25 '17 at 7:10
source share

As stated in the mongoose documentation and in Benjamin's answer, the Model.count () method is deprecated. Instead of using count (), the following options are possible:

Model.countDocuments (filterObject, callback)

Counts how many documents match the filter in the collection. Passing an empty object {} as a filter performs a full scan of the collection. If the collection is large, you can use the following method.

Model.estimatedDocumentCount ()

This model method estimates the number of documents in the MongoDB collection. This method is faster than the previous countDocuments (), because it uses collection metadata, rather than scrolling through the entire collection. However, as the name of the method implies, and depending on the configuration of the database, the result is estimated, since metadata may not reflect the actual number of documents in the collection at the time the method was executed.

Both methods return a mongoose request object, which can be performed in one of the following two ways. Use .exec () if you want to execute the request later.

1) pass callback function

For example, count all documents in a collection using .countDocuments ():

 SomeModel.countDocuments({}, function(err, count) { if (err) { return handleError(err) } //handle possible errors console.log(count) //and do some other fancy stuff }) 

Or count all the documents in the collection that have a specific name using .countDocuments ():

 SomeModel.countDocuments({ name: 'Snow' }, function(err, count) { //see other example } 

2) Use .then ()

The mongoose request has .then (), so it can be used. This is for convenience, and the request itself is not a promise.

For example, count all documents in a collection using .estimatedDocumentCount ():

 SomeModel .estimatedDocumentCount() .then(count => { console.log(count) //and do one super neat trick }) .catch(err => { //handle possible errors }) 

Hope this helps!

+7
Oct 31 '18 at 14:21
source share

As already mentioned, the code will not work as it is. The solution to this problem will be to use the callback function, but if you think that it will take you to the "Callback hell", you can find "Promisses".

Possible solution using callback function:

 //DECLARE numberofDocs OUT OF FUNCTIONS var numberofDocs; userModel.count({}, setNumberofDocuments); //this search all DOcuments in a Collection 

if you want to find the number of documents based on the query, you can do this:

  userModel.count({yourQueryGoesHere}, setNumberofDocuments); 

setNumberofDocuments is a split function:

 var setNumberofDocuments = function(err, count){ if(err) return handleError(err); numberofDocs = count; }; 

Now you can get the number of documents anywhere using the getFunction function:

  function getNumberofDocs(){ return numberofDocs; } var number = getNumberofDocs(); 

In addition, you use this asynchronous function inside synchronous with a callback, for example:

 function calculateNumberOfDoc(someParameter, setNumberofDocuments){ userModel.count({}, setNumberofDocuments); //this search all DOcuments in a Collection setNumberofDocuments(true); } 

Hope this helps others. :)

-one
Jul 17 '14 at 16:07
source share



All Articles