首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >按日期聚合排序mongodb

按日期聚合排序mongodb
EN

Stack Overflow用户
提问于 2017-11-12 07:47:08
回答 1查看 2.4K关注 0票数 0

我试图使用聚合按日期对文档进行排序。但我没办法。我的尝试如下。我是不是遗漏了什么?

代码语言:javascript
运行
复制
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 + '\'' +
            "}";
    }
}

样本文件

代码语言:javascript
运行
复制
{
    "_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"
    } }
EN

Stack Overflow用户

回答已采纳

发布于 2017-11-12 12:12:48

我认为你的$group$sort都有错误

从您粘贴的示例文档中,我无法判断您的last_access_time实际存储在哪里,但是从您的模型(AgentUserLogs)看来,您似乎已经将它与在字段user_data下可以找到的其余字段一起存储了。这是最后一个错误的分组字段:

代码语言:javascript
运行
复制
and("last_access_time", "$meta_data.last_access_time")

我相信你想写的是:

代码语言:javascript
运行
复制
and("last_access_time", "$meta_data.user_data.last_access_time")

然后在您的$sort中,因为您是按它分组的,这意味着它可以通过_id.last_access_time访问。因此,最后一个排序管道必须如下所示:

代码语言:javascript
运行
复制
sort(Sort.Direction.DESC, "_id.last_access_time")

根据您的代码,聚合管道应该如下所示(这只是一个例子):

代码语言:javascript
运行
复制
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
        }
    }
])
票数 1
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47246626

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档