0
집계를 사용하여 날짜순으로 문서를 정렬하려고합니다. 그러나 나는 할 수 없었다. 내 시도는 아래와 같습니다. 내가 놓친 게 있니?날짜별로 정렬 집계 mongodb
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 + '\'' +
"}";
}
}
샘플 문서
{
"_id" : ObjectId("5a0698755a640c6324a17581"),
"bot_id" : "1",
"page_id" : "2039339889632748",
"user_id" : "1258922750901107"
"meta_data" :
"user_data" : {
"first_name" : "Januka",
"last_name" : "Samaranayake",
"profile_pic" : "https://scontent.xx.fbcdn.net/v/t1.0-1/23172506_1725189057492533_3460235097206138375_n.jpg?oh=5183e7dd4e8ac49a49491055c24696d6&oe=5AA59955",
},
},
"live_agent" : {
"last_message" : {
"time" : "Sun Nov 12 12:24:53 IST 2017",
"message" : "hh",
"status" : "notRead"
},
"thread" : [
{
"from" : "user",
"time" : "Sat Nov 11 15:23:10 IST 2017",
"message" : {
"message" : "Default",
"type" : "init"
}
},
{
"from" : "user",
"time" : "Sun Nov 12 11:08:55 IST 2017",
"message" : {
"message" : "hi",
"type" : "text"
}
},
{
"from" : "agent",
"time" : "Sun Nov 12 11:38:14 IST 2017",
"message" : {
"message" : "hello",
"type" : "text"
}
},
{
"from" : "agent",
"time" : "Sun Nov 12 11:42:31 IST 2017",
"message" : {
"message" : "hi",
"type" : "text"
}
},
{
"from" : "agent",
"time" : "Sun Nov 12 12:23:31 IST 2017",
"message" : {
"message" : "hi",
"type" : "text"
}
},
{
"from" : "user",
"time" : "Sun Nov 12 12:24:53 IST 2017",
"message" : {
"message" : "hh",
"type" : "text"
}
}
],
"connect" : false,
"status" : "New"
} }
당신이 기대하고 무엇 ? 이는 단순히 필드 목록 만 "그룹화"할 것을 요청하는 것입니다. 그것들은 모두'_id' 밑에 그룹화되어 있습니다. 그래서 그것은 "_id.last_access_time"'입니다. 그러나 어쨌든 이것이 실제로 당신이하고자하는 쿼리라고 생각하지 않습니다. –
내가하려는 것은 위의 필드가있는 문서를 그룹화하고 "last_access_time"결과를 정렬하는 것입니다. –
"그룹"이란 무엇을 "생각"한다고 생각하십니까? 실제로 일어나고있는 일을 의도한다면 올바른 이름으로 정렬하면 실제로 작동하게됩니다. 그렇지 않으면 실제로 "그룹"은 실제로 무엇을 의미하는지 생각하는 것과 다른 의미를 갖습니다. –