我想用where子句在MongoDB中创建一个MAX查询。
我尝试这个
db.Collection.aggregate({$group : {_id : "$P_ID", massi : {$max : "$b_val"}}},{P_ID:'XYZ',experiment:'abc'});
我的WHERE子句是 {P_ID:'XYZ',experiment:'abc'}
我不为什么,但是此查询不起作用获取此错误:
assert: command failed: {
"ok" : 0,
"errmsg" : "A pipeline stage specification object must contain exactly one field.",
"code" : 16435
} : aggregate failed
_getErrorWithCode@src/mongo/shell/utils.js:25:13
doassert@src/mongo/shell/assert.js:13:14
assert.commandWorked@src/mongo/shell/assert.js:287:5
DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1312:5
@(shell):1:1
2016-10-15T11:55:31.459+0200 E QUERY [thread1] Error: command failed: {
"ok" : 0,
"errmsg" : "A pipeline stage specification object must contain exactly one field.",
"code" : 16435
} : aggregate failed :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
doassert@src/mongo/shell/assert.js:13:14
assert.commandWorked@src/mongo/shell/assert.js:287:5
DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1312:5
@(shell):1:1
Where子句必须处于$match
阶段。而且,最好将管道作为数组而不是单独的参数。
查询必须看起来像这样。
db.Collection.aggregate([
{
$match : {P_ID:'XYZ',experiment:'abc'}
},
{
$group : {
_id : "$P_ID",
massi : {$max : "$b_val"}
}
}
]);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句