node.js 如何获取文件 Mongoose 的数量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29532742/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get number of document Mongoose?
提问by kaizer
I'm working on a Nodejs/Express/Mongoose app, and I wanted to implement an autoincrement ID features by incrementing the number of recorded documents, but I'm not able to get this count, cause Mongoose 'count' method doesn't return the number:
我正在开发一个 Nodejs/Express/Mongoose 应用程序,我想通过增加记录文档的数量来实现自动递增 ID 功能,但我无法获得这个计数,因为 Mongoose 'count' 方法没有返回号码:
var number = Model.count({}, function(count){ return count;});
Is someone managed to get the count ? Help please.
有人设法得到计数吗?请帮忙。
回答by chridam
The count function is asynchronous, it doesn't synchronously return a value. Example usage:
count 函数是异步的,它不会同步返回值。用法示例:
Model.count({}, function(err, count){
console.log( "Number of docs: ", count );
});
You can also try chaining it after a find():
您也可以尝试在 a 之后链接它find():
Model.find().count(function(err, count){
console.log("Number of docs: ", count );
});
UPDATE:
更新:
As suggested by @Creynders, if you are trying to implement an auto incremental value then it would be worth looking at the mongoose-auto-incrementplugin:
正如@Creynders 所建议的,如果您尝试实现自动增量值,那么值得查看mongoose-auto-increment插件:
Example usage:
用法示例:
var Book = connection.model('Book', bookSchema);
Book.nextCount(function(err, count) {
// count === 0 -> true
var book = new Book();
book.save(function(err) {
// book._id === 0 -> true
book.nextCount(function(err, count) {
// count === 1 -> true
});
});
});
回答by Vasyl Boroviak
If you are using node.js >= 8.0 and Mongoose >= 4.0 you should use await.
如果你使用 node.js >= 8.0 和 Mongoose >= 4.0 你应该使用await.
const number = await Model.countDocuments();
console.log(number);
回答by Len Joseph
If anyone's checkin this out in 2019, countis deprecated. Instead, use countDocuments.
如果有人在 2019 年签入,则已count弃用。相反,使用countDocuments.
Example:
例子:
const count = await Model.countDocuments({
filterVar: parameter
});
console.log(count);
const count = await Model.countDocuments({
filterVar: parameter
});
console.log(count);
回答by Art
If your collection is large - use Model.estimatedDocumentCount(). It's faster then countand countDocuments, because it doesn't scan the entire collection.
如果您的收藏量很大,请使用Model.estimatedDocumentCount(). 它比countand更快countDocuments,因为它不会扫描整个集合。
See https://mongoosejs.com/docs/api/model.html#model_Model.estimatedDocumentCount
请参阅https://mongoosejs.com/docs/api/model.html#model_Model.estimatedDocumentCount
回答by JoGoFo
It looks like you are expecting var numberto contain the count value. In your callback function you are returning countbut this is executed asynchronously so will not assign the value to anything.
看起来您希望var number包含计数值。在您的回调函数中,您正在返回,count但这是异步执行的,因此不会将值分配给任何东西。
Also, your first parameter in your callback function should be err.
此外,回调函数中的第一个参数应该是err.
For example:
例如:
var number = Model.count({}, function(err, count) {
console.log(count); // this will print the count to console
});
console.log(number); // This will NOT print the count to console
回答by hussam
you hava to wait the callback function
你必须等待回调函数
Model.count({}, function(err , count){
var number = count;
console.log(number);
});
in JavaScript
在 JavaScript 中
setTimeout(function() {
console.log('a');
}, 0);
console.log("b");
the "b" will be printed before "a" because
“b”将在“a”之前打印,因为
console.log('a')

