mongodb 中带有 where 子句的最大查询
Max query in mongodb with where clause
我想在 MongoDB 中使用 where 子句创建一个 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"}
}
}
]);
如果您确定您的查询只会给您一条记录,您可以使用排序和限制来获得这样的结果。
db.collection.find({P_id:"XYZ",experiment:'abc'}).sort({"bVal": -1}).limit(1)
我想在 MongoDB 中使用 where 子句创建一个 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"}
}
}
]);
如果您确定您的查询只会给您一条记录,您可以使用排序和限制来获得这样的结果。
db.collection.find({P_id:"XYZ",experiment:'abc'}).sort({"bVal": -1}).limit(1)