Javascript MongoDb 聚合 $match 错误:“参数必须是聚合管道运算符”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26399989/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MongoDb aggregation $match error : "Arguments must be aggregate pipeline operators"
提问by Lazy
I can get all stats of the site with aggregationbut I want to it for a certain user, like $where.
我可以使用该网站的所有统计信息,aggregation但我想为某个用户使用它,例如$where.
All stats:
所有统计:
games.aggregate([{
$group: {
_id: '$id',
game_total: { $sum: '$game_amount'},
game_total_profit: { $sum: '$game_profit'}}
}]).exec(function ( e, d ) {
console.log( d )
})
When I try to use $matchoperator, I'm getting error :
当我尝试使用$match运算符时,出现错误:
games.aggregate([{
$match: { '$game_user_id' : '12345789' },
$group: {
_id: '$id',
game_total: { $sum: '$game_amount'},
game_total_profit: { $sum: '$game_profit'}}
}]).exec(function ( e, d ) {
console.log( d )
})
Arguments must be aggregate pipeline operators
What am I missing?
我错过了什么?
回答by Neil Lunn
Pipeline stages are separate BSON documents in the array:
管道阶段是数组中单独的 BSON 文档:
games.aggregate([
{ $match: { 'game_user_id' : '12345789' } },
{ $group: {
_id: '$id',
game_total: { $sum: '$game_amount'},
game_total_profit: { $sum: '$game_profit'}}
}}
]).exec(function ( e, d ) {
console.log( d )
});
So the Array or []bracket notation in JavaScript means it expects a "list" to be provided. This means a list of "documents" which are generally specified in JSON notation with {}braces.
因此[]JavaScript 中的数组或括号表示法意味着它需要提供一个“列表”。这意味着通常用带{}大括号的JSON 表示法指定的“文档”列表。
回答by Vishwadeep Kapoor
One more possible reason for this can be incompatible mongodb version and mongoose version used.
另一个可能的原因可能是使用的 mongodb 版本和 mongoose 版本不兼容。
In my case
就我而言
mongodb version: MongoDB shell version v3.6.18
mongodb 版本:MongoDB shell 版本 v3.6.18
mongoose version: "mongoose": "^5.1.3",
猫鼬版本:“猫鼬”:“^5.1.3”,
Please find the list of compatible version
请找到兼容版本列表

