使用mongoose和node.js获取mLab数据库中的文档总数

时间:2022-02-05 02:33:03

UPDATE

UPDATE

After some testing around, this works:

经过一些测试,这有效:

Url.count({}, (err, data) => {
    console.log(data); // prints correct number
  });

But this doesn't:

但这不是:

let len;
Url.count({}, (err, data) => {
  len = data;
});
console.log(len); // prints undefined

Is there a way to get the length data and then use it in subsequent operations?

有没有办法获取长度数据,然后在后续操作中使用它?

END UPDATE

结束更新

I'm a beginner with Node and Mongoose. How can I get the total number of documents in my database collection (which is called "Url")?

我是Node和Mongoose的初学者。如何获取数据库集合中的文档总数(称为“Url”)?

Some code I have tried:

我试过的一些代码:

1.

1。

let len = Url.count();

2.

2。

let len = Url.count({});

3.

3。

let len = Url.find().count();

4.

4。

let len = Url.find().count({});

5.

5。

let len = function(done) {
    Url.count({}, (err, data) => {
      if (err) { done(err) }
      else { done(null, data) }
    });
};

I'm looking to get back a number, but when I console.log(len), I get a huge object that goes on for lines and lines: 使用mongoose和node.js获取mLab数据库中的文档总数

我想找回一个数字,但是当我在console.log(len)时,我得到了一个巨大的对象,用于行和行:

2 个解决方案

#1


1  

I have struggled with this as well. Javascript asynchronous design at play here. Console.log() even before the query finishes executes the callback.

我也在努力解决这个问题。这里有Javascript异步设计。即使在查询完成之前,Console.log()也会执行回调。

So, you can either do this:

所以,你可以这样做:

let len;
Url.count({}, (err, data) => {
  len = data;
  console.log(len);
});

Or, wrap the whole thing around in an async function:

或者,在异步函数中包装整个事物:

// Just in case :)
mongoose.Promise = global.Promise;

async function dbOperations() {
  let len;
  // Ideally, you must also include error catching.
  // I am not, for now.
  len = await Url.count({}).exec();
  console.log(len);
}
dbOperations();

Please let me know if this works.

如果有效,请告诉我。

#2


1  

This is becuase of the order of operations happening. First this happens

这是因为正在发生的操作顺序。首先,这发生了

let len;

then

然后

Url.count({}, (err, data) => {
// Nothing is happening in here yet, still waiting to finish counting
});

then

然后

console.log(len); // So len IS undefined here. 

Finally, once it has finished counting:

最后,一旦完成计数:

len = data;

Luckily mongoose can help you here as they support .then

幸运的是猫鼬可以帮助你,因为他们支持。然后

const numberOfUrls = Url.count({});
numberOfUrls.then(number => {
    // Now you can do whatever you need to do with the number
    console.log('number', number)
});

just one important point,

只有一个重点,

Mongoose queries are not promises. They have a .then() function for co and async/await as a convenience. If you need a fully-fledged promise, use the .exec() function.

Mongoose查询不是承诺。它们有co和async / await的.then()函数作为方便。如果您需要完全成熟的承诺,请使用.exec()函数。

http://mongoosejs.com/docs/promises.html

http://mongoosejs.com/docs/promises.html

Hope that helps!

希望有所帮助!

#1


1  

I have struggled with this as well. Javascript asynchronous design at play here. Console.log() even before the query finishes executes the callback.

我也在努力解决这个问题。这里有Javascript异步设计。即使在查询完成之前,Console.log()也会执行回调。

So, you can either do this:

所以,你可以这样做:

let len;
Url.count({}, (err, data) => {
  len = data;
  console.log(len);
});

Or, wrap the whole thing around in an async function:

或者,在异步函数中包装整个事物:

// Just in case :)
mongoose.Promise = global.Promise;

async function dbOperations() {
  let len;
  // Ideally, you must also include error catching.
  // I am not, for now.
  len = await Url.count({}).exec();
  console.log(len);
}
dbOperations();

Please let me know if this works.

如果有效,请告诉我。

#2


1  

This is becuase of the order of operations happening. First this happens

这是因为正在发生的操作顺序。首先,这发生了

let len;

then

然后

Url.count({}, (err, data) => {
// Nothing is happening in here yet, still waiting to finish counting
});

then

然后

console.log(len); // So len IS undefined here. 

Finally, once it has finished counting:

最后,一旦完成计数:

len = data;

Luckily mongoose can help you here as they support .then

幸运的是猫鼬可以帮助你,因为他们支持。然后

const numberOfUrls = Url.count({});
numberOfUrls.then(number => {
    // Now you can do whatever you need to do with the number
    console.log('number', number)
});

just one important point,

只有一个重点,

Mongoose queries are not promises. They have a .then() function for co and async/await as a convenience. If you need a fully-fledged promise, use the .exec() function.

Mongoose查询不是承诺。它们有co和async / await的.then()函数作为方便。如果您需要完全成熟的承诺,请使用.exec()函数。

http://mongoosejs.com/docs/promises.html

http://mongoosejs.com/docs/promises.html

Hope that helps!

希望有所帮助!