带有Java聚合驱动程序的MongoDB查询和Java代码
有人能帮我告诉我下面对多个帐户的查询是否正确,这样我就可以从我的集合中一次性获取多个帐户的“mt”的总和吗 我尝试了以下查询:带有Java聚合驱动程序的MongoDB查询和Java代码,mongodb,aggregation-framework,Mongodb,Aggregation Framework,有人能帮我告诉我下面对多个帐户的查询是否正确,这样我就可以从我的集合中一次性获取多个帐户的“mt”的总和吗 我尝试了以下查询: db.getCollection('XYZ').aggregate([ {$match: { $and:[{ $or: [ {"try.Ind":"A","_id.a":"BB","_id.b":"HXYZ","_id.
db.getCollection('XYZ').aggregate([
{$match: {
$and:[{
$or: [
{"try.Ind":"A","_id.a":"BB","_id.b":"HXYZ","_id.c":"12345","_id.d":"FG","_id.e": new Date("2013-10-01T00:00:00.000Z")},
{"try.Ind":"A","_id.a":"AB","_id.b":"BBBT","_id.c":"23457","_id.d":"DA","_id.e":new Date("2013-10-01T00:00:00.000Z")}
]
}]
}
},
{$project:
{
"try": 1,
total:{
$sum:{
$let: {
vars: {
array:{
$filter:{
input:"$try",
cond:{$and:[{$eq:["$$this.Ind","A"]},{$gt:["$$this.mt",0.0]}]}
}
}},
"in":"$$array.mt"
}
}
}
}
}
])
MyCollection数据:
/* 1 */
{
"_id" : {
"a" : "NA",
"b" : "HXYZ",
"c" : "12345",
"d" : "CA",
"e" : "2018-03-09",
},
"try" : [
{
"Ind" : "A",
"mt" : 2.0,
},
{
"Ind" : "B",
"mt" : 3.0,
},
{
"Ind" : "A",
"mt" : 4.0,
},
{
"Ind" : "B",
"mt" : 5.0,
},
{
"Ind" : "A",
"mt" : 6.0,
},
{
"Ind" : "B",
"mt" : 7.0,
}
]
}
/* 2*/
{
"_id" : {
"a" : "AA",
"b" : "ACDE",
"c" : "45678",
"d" : "AB",
"e" : "2018-03-09",
},
"try" : [
{
"Ind" : "A",
"mt" : 2.0,
},
{
"Ind" : "B",
"mt" : 3.0,
},
{
"Ind" : "A",
"mt" : 4.0,
},
{
"Ind" : "B",
"mt" : 5.0,
},
{
"Ind" : "A",
"mt" : 6.0,
},
{
"Ind" : "B",
"mt" : 7.0,
}
]
}
/* 3 */
{
"_id" : {
"a" : "BB",
"b" : "BBBY",
"c" : "89012",
"d" : "BA",
"e" : "2004-02-12",
},
"try" : [
{
"Ind" : "A",
"mt" : 2.0,
},
{
"Ind" : "B",
"mt" : 3.0,
},
{
"Ind" : "A",
"mt" : 4.0,
},
{
"Ind" : "B",
"mt" : 5.0,
},
{
"Ind" : "A",
"mt" : 6.0,
},
{
"Ind" : "B",
"mt" : 7.0,
}
]
}
预期产出:
如果Ind=“A”和mt>0.0
账户12345
2.0+4.0+6.0=12
账户89012
2.0+4.0+6.0=12
等等
输出将是下面的lyk
{
{
"_id": {
"a" : "NA",
"b" : "HXYZ",
"c" : "12345",
"d" : "CA",
"e" : "2018-03-09",
},
"total": 12
},{ "_id": {
"a" : "BB",
"b" : "BBBY",
"c" : "89012",
"d" : "BA",
"e" : "2004-02-12",
},
"total" :12
}
}
如果上述查询适用于多个帐户,并从集合中一次性获取所有帐户的总和,请更正我的问题
我还需要为上述查询使用聚合的相应java代码,特别是匹配条件
提前感谢 你的问题是正确的!可能重复的