如何对嵌套结构的Mongodb查询进行排序

时间:2016-02-15 09:38:29

标签: mongodb sorting pymongo pymongo-3.x

我有一堵墙和一些与墙相关的评论,

{
    "Wall_id": "wall002",
    "user_id": "user002",
    "message_author": "AuthorB",
    "wall_message": "content:TRY",
    "posted_on": "24/8/15",
    "author_id": "A002",
    "path_to_files": ["home/path"],
    "wall_recepients":["user002","user003","user004","user005","user006"],
    "w_timestamp": 678,
    "Comments": [
        {
            "comment_id": "cmt001",
            "Wall_id": "wall001",
            "user_id": "user001",
            "pathtofile": "[home/doc/file]",
            "comment": "try 2",
            "profile_pic": "home2/pic.jpg",
            "user_name": "user2",
            "c_timestamp": 700
       },
        {
            "comment_id": "cmt001",
            "Wall_id": "wall001",
            "user_id": "user001",
            "pathtofile": "[home/doc/file]",
            "comment": "try 2",
            "profile_pic": "home2/pic.jpg",
            "user_name": "user2",
            "c_timestamp": 1000
       }
    ]
}

我想要一个查询,根据基于c_timestamp的每个墙排序基于w_timestamp和comments数组排序的墙元素。

我正在使用pymongo并试过,

searchUser = "user002"
result = post.find({"wall_recepients":{"$in":[searchUser]}}).sort([["post.w_timestamp",DESCENDING],["post.Comments.c_timestamp",DESCENDING]])

DESCENDING是pymongo.DESCENDING

提前致谢! 预期产出:

给定用户ID,获取wall_recepients包含user_id的所有墙上项目,例如,

[

{
    "Wall_id": "wall002",
    "user_id": "user002",
    "message_author": "AuthorB",
    "wall_message": "content:TRY",
    "posted_on": "24/8/15",
    "author_id": "A002",
    "path_to_files": ["home/path"],
    "wall_recepients":["user002","user003","user004","user005","user006"],
    "w_timestamp": 678,
    "Comments": [
        {
            "comment_id": "cmt001",
            "Wall_id": "wall001",
            "user_id": "user001",
            "pathtofile": "[home/doc/file]",
            "comment": "try 2",
            "profile_pic": "home2/pic.jpg",
            "user_name": "user2",
            "c_timestamp": 700
       },
        {
            "comment_id": "cmt001",
            "Wall_id": "wall001",
            "user_id": "user001",
            "pathtofile": "[home/doc/file]",
            "comment": "try 2",
            "profile_pic": "home2/pic.jpg",
            "user_name": "user2",
            "c_timestamp": 1000
       }
    ]
},

{
    "Wall_id": "wall001",
    "user_id": "user002",
    "message_author": "AuthorB",
    "wall_message": "content:TRY",
    "posted_on": "24/8/15",
    "author_id": "A002",
    "path_to_files": ["home/path"],
    "wall_recepients":["user002","user003","user004","user005","user006"],
    "w_timestamp": 666,
    "Comments": [
        {
            "comment_id": "cmt001",
            "Wall_id": "wall001",
            "user_id": "user001",
            "pathtofile": "[home/doc/file]",
            "comment": "try 2",
            "profile_pic": "home2/pic.jpg",
            "user_name": "user2",
            "c_timestamp": 124
       },
        {
            "comment_id": "cmt001",
            "Wall_id": "wall001",
            "user_id": "user001",
            "pathtofile": "[home/doc/file]",
            "comment": "try 2",
            "profile_pic": "home2/pic.jpg",
            "user_name": "user2",
            "c_timestamp": 246
       }
    ]
}


]

0 个答案:

没有答案