MongoDB count()未定义 [英] MongoDB count() undefined
本文介绍了MongoDB count()未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在Javascript-NodeJS-MongoDB上非常新手,我尝试知道一个查询文件的数量。
I'm very newbie on Javascript-NodeJS-MongoDB, I try to know the number of documents found by a query.
...
var page = req.params.page;
var db = require('mongojs').connect('localhost:27017/foo', ['bar']);
var docs = db.bar.find({x:'MME'}).sort({y:1}).skip(10*(page-1)).limit(10);
var nbDocs = db.bar.find({x:'MME'}).count(); /*docs.count();*/
console.log(nbDocs);
但不幸的是,日志给了我'undefined',同样如果我代码
But unfortunately the log gives me 'undefined', the same if I code
var nbDocs = docs.count();
感谢您的宝贵帮助。
Gilles。
推荐答案
根据文档结果将在回调到 cursor.count()
方法。对于那些刚接触javascript的人来说,这可能会更有挑战性,但是我认为这样的事情应该是有效的:
According to the docs the result will be in the second argument of the callback to the cursor.count()
method. This might be a little more challenging to implement for somebody who's new to javascript, but I think something like this should work:
docs.count(function(error, nbDocs) {
// Do what you need the count for here.
});
这篇关于MongoDB count()未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文