gpt4 book ai didi

java - 复制按字段值分组

转载 作者:行者123 更新时间:2023-11-30 06:37:01 28 4
gpt4 key购买 nike

我正在尝试执行以下代码:

MongoDatabase db = MongoDatabaseConnector.getDatabase();
MongoCollection<Document> chatLogCollection = db.getCollection("chatLog");

AggregateIterable<Document> result = chatLogCollection.aggregate(Arrays.asList(
new Document("$group", new Document("_id", new Document("sessionId", "$sessionGUID").append("time", "$ts").append("makerID", "$makerID"))),
new Document("$sort", new Document("time", -1)),
new Document("$skip", skip),
new Document("$limit", limit)
));

正如我所料,输出不应具有重复的 sessionId 值,因为我正在使用 group by sessionId。但问题是结果输出具有重复的 sessionId 值。

[
{
"displayName": "Unauthenticated User",
"sessionId": "7b60615d-5909-1bf8-e5b9-6ee55e08452d",
"time": {
"$date": 1499759790117
},
"makerID": "NA"
},
{
"displayName": "Unauthenticated User",
"sessionId": "0a6b5db0-fecf-a7c2-9757-67e562b7e37e",
"time": {
"$date": 1499840350180
},
"makerID": "NA"
},
{
"displayName": "Unauthenticated User",
"sessionId": "0a6b5db0-fecf-a7c2-9757-67e562b7e37e",
"time": {
"$date": 1499840353438
},
"makerID": "NA"
}

]

最佳答案

尝试仅按 sessionId 进行分组,如下所示:

    AggregateIterable<Document> result = chatLogCollection.aggregate(Arrays.asList(
new Document("$group", new Document("_id", "$sessionGUID")
.append("time", new Document("$first", "$ts"))
.append("makerID", new Document("$first","$makerID"))),
new Document("$sort", new Document("time", -1)),
new Document("$skip", skip),
new Document("$limit", limit)
));

关于java - 复制按字段值分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45050672/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com