如何获取Mongoose文件的数量?

时间:2021-08-02 02:36:45

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.

有人设法得到了计数吗?请帮助。

4 个解决方案

#1


26  

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():

你也可以尝试在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-increment plugin:

正如@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 

        });
    });
});

#2


3  

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.count();
console.log(number);

#3


1  

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')

#4


0  

It looks like you are expecting var number to contain the count value. In your callback function you are returning count but 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.

此外,您的回调函数中的第一个参数应该是错误的。

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

#1


26  

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():

你也可以尝试在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-increment plugin:

正如@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 

        });
    });
});

#2


3  

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.count();
console.log(number);

#3


1  

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')

#4


0  

It looks like you are expecting var number to contain the count value. In your callback function you are returning count but 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.

此外,您的回调函数中的第一个参数应该是错误的。

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