使用mongo模板聚合以字符串形式检索_id

时间:2019-09-11 05:08:28

标签: java spring-data mongotemplate

我有聚合查询以基于ID从几个集合中检索数据。从mongo模板集合中获取文档总是将id作为时间戳返回给我-我如何确保我将不仅对一个集合而是对所有集合都将_id作为String获取

以下是汇总:

 Aggregation.newAggregation( Aggregation.match(criteria),
                LookupOperation.newLookup()
                        .from("First")
                        .localField("_id")
                        .foreignField("masterId")
                        .as("First"),
                LookupOperation.newLookup()
                        .from("Second")
                        .localField("_id")
                        .foreignField("masterId")
                        .as("Second"),
                LookupOperation.newLookup()
                        .from("Third")
                        .localField("_id")
                        .foreignField("masterId")
                        .as("Third"),
                LookupOperation.newLookup()
                        .from("Fourth")
                        .localField("_id")
                        .foreignField("masterId")
                        .as("Fourth"),
                LookupOperation.newLookup()
                        .from("Fifth")
                        .localField("_id")
                        .foreignField("masterId")
                        .as("Fifth"),
                LookupOperation.newLookup()
                        .from("Sixth")
                        .localField("_id")
                        .foreignField("masterId")
                        .as("Sixth"))

对Mongo模板的操作类似于:

mongoTemplate.aggregate(aggregation, "collection", Document.class).getRawResults()

Always gives me result as 
{
    "_id": {
        "timestamp": 1568176455,
        "machineIdentifier": 4596927,
        "processIdentifier": 18068,
        "counter": 8470464,
        "timeSecond": 1568176455,
        "time": 1568176455000,
        "date": "2019-09-11T04:34:15.000+0000"
    },
        "First": [
        {
            "_id": {
                "timestamp": 1568176475,
                "machineIdentifier": 4596927,
                "processIdentifier": 18068,
                "counter": 8470706,
                "timeSecond": 1568176475,
                "time": 1568176475000,
                "date": "2019-09-11T04:34:35.000+0000"
            },
            "data": {

            }
        }
    ],
    "Second": [
        {
            "_id": {
                "timestamp": 1568176465,
                "machineIdentifier": 4596927,
                "processIdentifier": 18068,
                "counter": 8470544,
                "timeSecond": 1568176465,
                "time": 1568176465000,
                "date": "2019-09-11T04:34:25.000+0000"
            },
            "data": {

            }
        }
    ],
    //and so on
    ]
}


 I would like to get all _id as String instead of timestamp. Is there any I can set that on mongo template level ?

0 个答案:

没有答案