Java 按日期聚合排序mongodb
我正在尝试使用聚合按日期对文档进行排序。但是我不能。我的尝试如下。我错过什么了吗Java 按日期聚合排序mongodb,java,mongodb,spring-boot,aggregation,Java,Mongodb,Spring Boot,Aggregation,我正在尝试使用聚合按日期对文档进行排序。但是我不能。我的尝试如下。我错过什么了吗 public static JSONArray get_users_from_db(String botId, String pageId, MongoTemplate mongoTemplate) throws Exception { AggregationResults<AgentUserLogs> groupResults = mongoTemplate.aggregate(mak
public static JSONArray get_users_from_db(String botId, String pageId, MongoTemplate mongoTemplate) throws Exception {
AggregationResults<AgentUserLogs> groupResults = mongoTemplate.aggregate(makeQuery(botId, pageId), "chatuser_log", AgentUserLogs.class);
List<AgentUserLogs> list = groupResults.getMappedResults();
JSONArray array = new JSONArray();
Gson gson = new Gson();
for (AgentUserLogs obj : list) {
array.put(new JSONObject(gson.toJson(obj)));
}
return array;
}
private static Aggregation makeQuery(String botId, String pageId) {
return newAggregation(
match(Criteria.where("bot_id").is(botId).and("page_id").is(pageId)),
group(Fields.fields().and("first_name", "$meta_data.user_data.first_name").and("last_name", "$meta_data.user_data.last_name").and("profile_pic", "$meta_data.user_data.profile_pic").and("user_id", "$user_id").and("last_message", "$live_agent.last_message").and("last_access_time", "$meta_data.last_access_time")),
sort(Sort.Direction.DESC, "last_access_time")
);
}
public class AgentUserLogs {
private String first_name;
private String last_name;
private String profile_pic;
private String user_id;
private Instant last_access_time;
private Object last_message;
@Override
public String toString() {
return "{" +
"first_name='" + first_name + '\'' +
"last_name='" + last_name + '\'' +
"profile_pic='" + profile_pic + '\'' +
"user_id='" + user_id + '\'' +
"last_access_time='" + last_access_time + '\'' +
"last_message='" + last_message + '\'' +
"}";
}
}
我认为你的$group和$sort都有一个错误 从您粘贴的示例文档中,我无法告诉您的上次访问时间实际存储在何处,但从您的模型代理日志中,您似乎已将其与可在“用户数据”字段下找到的其余字段一起存储。 这是错误的最后一个分组字段:
and("last_access_time", "$meta_data.last_access_time")
我相信你想写的是:
and("last_access_time", "$meta_data.user_data.last_access_time")
然后在$sort中,由于您是按它分组的,这意味着可以通过_id.last_access_time访问它。因此,最后一个排序管道必须如下所示:
sort(Sort.Direction.DESC, "_id.last_access_time")
根据您的代码,您的聚合管道应如下所示—这只是一个示例:
db.getCollection('yourCollectionName').aggregate([{
$match: {
"bot_id": "1",
"page_id": "2039339889632748"
}
},
{
$group: {
_id: {
first_name: "$meta_data.user_data.first_name",
last_name: "$meta_data.user_data.last_name",
profile_pic: "$meta_data.user_data.profile_pic",
user_id: "$user_id",
last_message: "$live_agent.last_message",
last_access_time: "$meta_data.user_data.last_access_time"
}
}
},
{
$sort: {
"_id.last_access_time": -1
}
}
])
你在期待什么?这只是要求按字段列表进行分组,而不是其他。这些都归在_id顺便说一句。所以这是_id.最后一次访问时间。但不知何故,我不认为这实际上是你想要做的查询。我试图做的是用上述字段对文档进行分组,并在上次访问时对结果进行排序。你认为分组是什么意思?如果你真的想知道发生了什么,那么按照我给你的正确命名进行排序实际上是可行的。否则,你们们的意思和你们们所认为的群体的实际行为不同。
db.getCollection('yourCollectionName').aggregate([{
$match: {
"bot_id": "1",
"page_id": "2039339889632748"
}
},
{
$group: {
_id: {
first_name: "$meta_data.user_data.first_name",
last_name: "$meta_data.user_data.last_name",
profile_pic: "$meta_data.user_data.profile_pic",
user_id: "$user_id",
last_message: "$live_agent.last_message",
last_access_time: "$meta_data.user_data.last_access_time"
}
}
},
{
$sort: {
"_id.last_access_time": -1
}
}
])