MongoDb 聚合出错
Getting error on MongoDb aggregate
我一直在本地构建我的应用程序,一切正常,它使用最新的 MongoDB 3.4,我的聚合调用工作正常。
app.get('/random_menu', function (req, res) {
Menus.aggregate([{$sample: {size: 1}}], function (err, data) {
res.json(data);
});
});
我现在已将我的代码移至我的 raspberry pi,它仅限于 MongoDB 2.4.10,而且我的聚合函数似乎无法在其上运行。我的应用程序没有 return 任何数据,也没有错误。
为了测试,我通过 RoboMongo 进行了测试,使用以下命令,它在本地 3.4 版本上运行良好:
db.getCollection('menus').aggregate([{$sample: {size: 1}}])
但是当我通过 RoboMongo 在 pi db 上尝试时,出现以下错误:
assert: command failed: {
"errmsg" : "Pipeline::parseCommand(): unrecognized field \"cursor",
"ok" : 0
} : aggregate failed
_getErrorWithCode@src/mongo/shell/utils.js:23:13
doassert@src/mongo/shell/assert.js:13:14
assert.commandWorked@src/mongo/shell/assert.js:266:5
DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1215:5
@(shell):1:1
Error: command failed: {
"errmsg" : "Pipeline::parseCommand(): unrecognized field \"cursor",
"ok" : 0
} : aggregate failed :
_getErrorWithCode@src/mongo/shell/utils.js:23:13
doassert@src/mongo/shell/assert.js:13:14
assert.commandWorked@src/mongo/shell/assert.js:266:5
DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1215:5
@(shell):1:1
我试过用谷歌搜索这个但似乎找不到任何东西。我是 MongoDB 的新手,所以任何意见都将不胜感激。
$sample 从 v3.2 开始可用。
MongoDB 2.4 于 2013 年 3 月首次发布,并于 2016 年 3 月结束生命周期(比 MongoDB 3.4.0 发布早六个月)。针对较新版本进行测试可能会导致意外使用向后不兼容的功能或 API。除了缺少的功能,停产的服务器版本可能缺少重要的错误修复和安全改进,并且将不再针对当前的驱动程序版本进行测试。
您的问题突出了 MongoDB 2.4 中不存在的两个功能问题:
$sample
聚合阶段,在 MongoDB 3.2 中添加。
作为使用 3.2 之前版本的 MongoDB 进行采样的解决方法,我建议对旧版本的 MongoDB 使用 NPM 包 mongodb-collection-sample
in your application code. This uses $sample
if supported or a reservoir sampling 算法。
基于游标的聚合,在 MongoDB 2.6.
中添加
unrecognized field "cursor"
错误表明了这一点。 MongoDB 2.6+ 驱动程序默认使用游标来迭代大型结果集;旧版本仅限于内联结果(最多 16MB)。您的 Robomongo 版本嵌入了比 MongoDB 2.4 更新的 shell(您可以使用 version()
进行检查)并且似乎不支持使用 aggregate()
的 2.4 样式聚合查询帮手。有替代语法通过 db.runCommand()
调用 aggregate
而不是 aggregate()
助手,但升级到受支持的服务器版本将是更好的方法。
如果您是 MongoDB 的新手,我绝对鼓励您使用受支持的版本(MongoDB 3.0 或更新于 2017 年 3 月)并在您的开发和部署环境中使用相同的主要版本在可能的情况下。
从 MongoDB 3.4 开始,Raspberry Pi 不是官方支持的平台。但是,其他人已经取得了成功 community packages for ArchLinux ARM。 Raspberry Pi 有限的硬件资源通常不适合繁重的工作,因此可以考虑使用 Pi 来 运行 您的 Node 应用程序,但连接到远程托管的数据库服务器。
我一直在本地构建我的应用程序,一切正常,它使用最新的 MongoDB 3.4,我的聚合调用工作正常。
app.get('/random_menu', function (req, res) {
Menus.aggregate([{$sample: {size: 1}}], function (err, data) {
res.json(data);
});
});
我现在已将我的代码移至我的 raspberry pi,它仅限于 MongoDB 2.4.10,而且我的聚合函数似乎无法在其上运行。我的应用程序没有 return 任何数据,也没有错误。
为了测试,我通过 RoboMongo 进行了测试,使用以下命令,它在本地 3.4 版本上运行良好:
db.getCollection('menus').aggregate([{$sample: {size: 1}}])
但是当我通过 RoboMongo 在 pi db 上尝试时,出现以下错误:
assert: command failed: {
"errmsg" : "Pipeline::parseCommand(): unrecognized field \"cursor",
"ok" : 0
} : aggregate failed
_getErrorWithCode@src/mongo/shell/utils.js:23:13
doassert@src/mongo/shell/assert.js:13:14
assert.commandWorked@src/mongo/shell/assert.js:266:5
DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1215:5
@(shell):1:1
Error: command failed: {
"errmsg" : "Pipeline::parseCommand(): unrecognized field \"cursor",
"ok" : 0
} : aggregate failed :
_getErrorWithCode@src/mongo/shell/utils.js:23:13
doassert@src/mongo/shell/assert.js:13:14
assert.commandWorked@src/mongo/shell/assert.js:266:5
DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1215:5
@(shell):1:1
我试过用谷歌搜索这个但似乎找不到任何东西。我是 MongoDB 的新手,所以任何意见都将不胜感激。
$sample 从 v3.2 开始可用。
MongoDB 2.4 于 2013 年 3 月首次发布,并于 2016 年 3 月结束生命周期(比 MongoDB 3.4.0 发布早六个月)。针对较新版本进行测试可能会导致意外使用向后不兼容的功能或 API。除了缺少的功能,停产的服务器版本可能缺少重要的错误修复和安全改进,并且将不再针对当前的驱动程序版本进行测试。
您的问题突出了 MongoDB 2.4 中不存在的两个功能问题:
$sample
聚合阶段,在 MongoDB 3.2 中添加。作为使用 3.2 之前版本的 MongoDB 进行采样的解决方法,我建议对旧版本的 MongoDB 使用 NPM 包
mongodb-collection-sample
in your application code. This uses$sample
if supported or a reservoir sampling 算法。基于游标的聚合,在 MongoDB 2.6.
中添加unrecognized field "cursor"
错误表明了这一点。 MongoDB 2.6+ 驱动程序默认使用游标来迭代大型结果集;旧版本仅限于内联结果(最多 16MB)。您的 Robomongo 版本嵌入了比 MongoDB 2.4 更新的 shell(您可以使用version()
进行检查)并且似乎不支持使用aggregate()
的 2.4 样式聚合查询帮手。有替代语法通过db.runCommand()
调用aggregate
而不是aggregate()
助手,但升级到受支持的服务器版本将是更好的方法。
如果您是 MongoDB 的新手,我绝对鼓励您使用受支持的版本(MongoDB 3.0 或更新于 2017 年 3 月)并在您的开发和部署环境中使用相同的主要版本在可能的情况下。
从 MongoDB 3.4 开始,Raspberry Pi 不是官方支持的平台。但是,其他人已经取得了成功 community packages for ArchLinux ARM。 Raspberry Pi 有限的硬件资源通常不适合繁重的工作,因此可以考虑使用 Pi 来 运行 您的 Node 应用程序,但连接到远程托管的数据库服务器。