gpt4 book ai didi

node.js - MongoDB count() 未定义

转载 作者:IT老高 更新时间:2023-10-28 13:06:19 26 4
gpt4 key购买 nike

我是 Javascript-NodeJS-MongoDB 的新手,我尝试知道查询找到的文档数量。

...
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);

但不幸的是,日志给了我“未定义”,如果我编码也一样

var nbDocs = docs.count();

非常感谢您的宝贵帮助。

吉尔斯。

最佳答案

根据the docs结果将在 cursor.count() 方法的回调的第二个参数中。对于刚接触 javascript 的人来说,这可能更具挑战性,但我认为这样的事情应该可行:

docs.count(function(error, nbDocs) {
// Do what you need the count for here.
});

关于node.js - MongoDB count() 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13148875/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com