How to get all count of mongoose model?
mongodb, mongoose, node.js
Solution
The code below works. Note 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);
});
}
});
Problem
How can I know the count of a model that data has been saved? there is a method of `Model.count()`, but it doesn't seem to 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` is an Object, which method called can get a real `count`? Thanks