我需要一个mongo数据库查询以及相应的Java代码查询使用聚合框架为下面提到的场景,
Scenario is :
I need to search an array for "seqNo": 4 based on "aC","aI","aN","aT","bID","pD" from collection A.
Please find the collection mentioned below,
Collection A:
/*1*/
{
"_id" : ObjectId("6398b904aa0c28d6193bb853"),
"aC" : "AB",
"aI" : "ABCD",
"aN" : "040000000002",
"aT" : "CA",
"bID" : NumberLong(0),
"pD" : "2019-04-19",
"timeToLive" : ISODate("2019-04-19T00:00:00.000Z"),
"transactions" : [
{
"amt" : NumberDecimal("-12.340"),
"seqNo" : 2,
"valDt" : "2022-10-04"
},
{
"amt" : NumberDecimal("-6.800"),
"seqNo" : 5,
"valDt" : "2022-10-04"
}
]
}
/*2*/
{
"_id" : ObjectId("5d42daa7decf182710080d46"),
"aC" : "AB",
"aI" : "ABCD",
"aN" : "040000000002",
"aT" : "CA",
"bID" : NumberLong(1),
"pD" : "2019-04-19",
"timeToLive" : ISODate("2019-04-19T00:00:00.000Z"),
"transactions" : [
{
"seqNo" : 4,
"amt" : NumberDecimal("26074.000"),
"valDt" : "2019-04-19"
},
{
"seqNo" : 3,
"amt" : NumberDecimal("26074.000"),
"valDt" : "2019-04-19"
}
]
}
请帮助我的查询,这将是非常有帮助的,如果详细解释。提前感谢。
1条答案
按热度按时间jm81lzqq1#
Java总是比python或javascript更冗长一些,但我是这样做的:因为我的编辑器匹配大括号和方括号,所以我发现将查询构造为JSON,然后将其转换为所需的
Document
对象管道更容易。如果您使用的是Java 13或更高版本,则
String
的文本块会使操作更加简单: