Spring Data mongoDb使用组操作和嵌套字段的聚合查询

u4dcyp6a  于 2023-01-16  发布在  Go
关注(0)|答案(1)|浏览(114)

我想使用spring data mongodb创建一个聚合查询。
我的收藏数据结构是:

{
    date: ISODate("2022-12-11T01:13:00.000Z"),
    metadata: {
        homeId: '51b87ea3d846f450141ae58z',
        nodeId: '51b87ea3d846f450141ae58z'
    },
    value: 42,
    _id: ObjectId("63b583e30c1e523313b64ed5")
}

我想翻译成java的MongoDb查询是:

db.consumptions.aggregate(
[
    { $match : {"metadata.nodeId": "51b87ea3d846f450141ae58z"}
    },
    { $project: { 
        date: { 
            $dateToParts: { date: "$date"}
            }, 
        value: 1
        }
    },
    { $group: { 
        _id: { 
            date: { 
                year: "$date.year", month: "$date.month", day: "$date.day"
            }
        }, 
        cumulValue: { $sum: "$value"}
        }
    },
    { $sort : { "_id.date": -1}
    }
]
)

MongoDb查询结果如下所示:

[
    {
        _id: { 
            date: { year: 2022, month: 12, day: 11 } 
        },
        cumulValue: 42 
    }
]

正如您在这里看到的,结果结构是**_id**,其中包含一个嵌套对象date,该对象包含字段year、month和day。
我希望使用groupOperation和spring data mongo得到相同的result _id结构。
我尝试了这个实现

MatchOperation matchOperation = Aggregation.match(
    new Criteria().andOperator(Criteria.where("metadata.nodeId").is(nodeId);
        
// projection Operation
ProjectionOperation projectionOperation = project("value")
    .and(DateOperators.dateOf("date").toParts()).as("date");

// group Operation
GroupOperation groupOperation = Aggregation.group(
    "date.year", "date.month", "date.day")
    .sum("value").as("cumulativeValue");
        
// Sort Operation
SortOperation sortOperation = sort(pageRequest.getSort());

Aggregation aggregation = Aggregation.newAggregation(
    matchOperation, projectionOperation, groupOperation,sortOperation);

但是我不能得到预期的结果结构(带有嵌套对象日期)

{
    _id: {
      year: 2022,
      month: 11,
      day: 11
    },
    cumulValue: 284
}
mnemlml8

mnemlml81#

您可以通过分析整个管道字符串来尝试此操作。

String groupStage = "{\r\n" + 
        "    $group: {\r\n" + 
        "      _id: {\r\n" + 
        "        date: {\r\n" + 
        "          year: \"$date.year\",\r\n" + 
        "          month: \"$date.month\",\r\n" + 
        "          day: \"$date.day\"\r\n" + 
        "        }\r\n" + 
        "      },\r\n" + 
        "      cumulValue: {\r\n" + 
        "        $sum: \"$value\"\r\n" + 
        "      }\r\n" + 
        "    }\r\n" + 
        "  }"

GroupOperation groupOperation = context -> context.getMappedObject(Document.parse(groupStage));

相关问题