如何对嵌套结构的Mongodb查询进行排序
我有一堵墙和几条关于墙的评论如何对嵌套结构的Mongodb查询进行排序,mongodb,sorting,pymongo,pymongo-3.x,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":["
{
"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
}
]
}
我需要一个查询,它可以对基于w_时间戳排序的墙元素进行排序,并对基于c_时间戳排序的每个墙的注释数组进行排序
我正在使用pymongo并尝试
searchUser = "user002"
result = post.find({"wall_recepients":{"$in":[searchUser]}}).sort([["post.w_timestamp",DESCENDING],["post.Comments.c_timestamp",DESCENDING]])
下降就是下降
提前谢谢!
预期产出:
给定一个用户id,获取wall\u Recipients包含该用户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
}
]
}
]
您的预期输出是什么?我只希望每个墙id按时间戳降序排序,并且在每个墙id中,我还希望注释按时间戳排序(ASC或DESC)。请使用您问题上的链接添加预期结果谢谢,我已添加预期输出,这有帮助吗?按评论排序是什么意思?是否要对
注释
数组进行排序?如果是,您为什么需要它?您的预期输出是什么?我只希望每个墙id按时间戳降序排序,并且在每个墙id中,我还希望注释按时间戳排序(ASC或DESC)。请使用您问题上的链接添加预期结果谢谢,我已添加预期输出,这有帮助吗?按评论排序是什么意思?是否要对注释
数组进行排序?如果是,为什么需要?