假设我们有以下文件:
{
reportName: 'ABC_REPORT'
reports: [
{
"startDate" : ISODate("2017-07-02T00:00:00Z"),
"endDate" : ISODate("2017-07-08T00:00:00Z"),
"data" : [ { DATA_ID: '0123404' }, { DATA_ID: '111101' }, { DATA_ID: '22201' } ]
},
{
"startDate" : ISODate("2017-07-09T00:00:00Z"),
"endDate" : ISODate("2017-07-15T00:00:00Z"),
"data" : [ { DATA_ID: '111103' } ] },
{
"startDate" : ISODate("2017-07-16T00:00:00Z"),
"endDate" : ISODate("2017-07-22T00:00:00Z"),
"data" : [{ DATA_ID: '0123403' }, { DATA_ID: '111109' }]
},
{
"startDate" : ISODate("2017-07-16T00:00:00Z"),
"endDate" : ISODate("2017-07-22T00:00:00Z"),
"data" : [{ DATA_ID: '0123402' }]
}
]}
现在我想编写一个查询,该查询应该搜索以(' 01234',' 1111',' 222')开头的DATA_ID。如果对于每个模式,它返回多个DATA_ID,那么结果应该只有最大的一个。 只有相同模式的最后两位数才会递增。
期望获得如下数组:
{
data: [{ DATA_ID: '111109' },
{ DATA_ID: '0123404' },
{ DATA_ID: '22201' }
]
}
Iam试图通过聚合框架来解决这个问题,但确实在努力解决这个问题。
答案 0 :(得分:0)
如果您需要最长的
db.col.aggregate([
{
$unwind:"$reports"
},
{
$unwind:"$reports.data"
},
{
$replaceRoot:{ newRoot:"$reports.data"}
},
{
$project:{
"DATA_ID":1,
"GRP_ID":{
$switch: {
branches: [
{ case: {$eq:[{ $indexOfBytes:["$DATA_ID","01234"]},0]}, then: "0124" },
{ case: {$eq:[{ $indexOfBytes:["$DATA_ID","xyz"]},0]}, then: "xyz" },
{ case: {$eq:[{ $indexOfBytes:["$DATA_ID","abcd"]},0]}, then: "abcd" }
],
default: ""
}
}
}
},
{
$group:{
_id:"$GRP_ID",
DATA_IDS:{$addToSet:"$DATA_ID"}
}
},
{
$project:{
_id:0,
DATA_ID:{
$reduce:{
input:"$DATA_IDS",
initialValue:"",
in:{$cond:[{$gte:[{$strLenCP:"$$this"},{$strLenCP:"$$value"}]},"$$this","$$value"]}
}
}
}
}
])
如果你需要最大的重量
db.col.aggregate([
{
$unwind:"$reports"
},
{
$unwind:"$reports.data"
},
{
$replaceRoot:{ newRoot:"$reports.data"}
},
{
$project:{
"DATA_ID":1,
"GRP_ID":{
$switch: {
branches: [
{ case: {$eq:[{ $indexOfBytes:["$DATA_ID","01234"]},0]}, then: "0124" },
{ case: {$eq:[{ $indexOfBytes:["$DATA_ID","xyz"]},0]}, then: "xyz" },
{ case: {$eq:[{ $indexOfBytes:["$DATA_ID","abcd"]},0]}, then: "abcd" }
],
default: ""
}
}
}
},
{
$group:{
_id:"$GRP_ID",
DATA_IDS:{$addToSet:"$DATA_ID"}
}
},
{
$project:{
_id:0,
DATA_ID:{
$reduce:{
input:"$DATA_IDS",
initialValue:"",
in:{$cond:[{$gte:["$$this","$$value"]},"$$this","$$value"]}
}
}
}
}
])