Mongodb mongoose按数组中多个对象的值排序
查找mongoDB的排序数据时出现问题。 这是我们的数据。 存在具有数组“eventList”的userDataMongodb mongoose按数组中多个对象的值排序,mongodb,mongoose,Mongodb,Mongoose,查找mongoDB的排序数据时出现问题。 这是我们的数据。 存在具有数组“eventList”的userData [ { "_id": "5a55eb107d4e4d5531d790e9", "id": "aa@aa.com", "intro": null, "name": "aaa", "eventList": [ { "eventId": "5a2e18f
[
{
"_id": "5a55eb107d4e4d5531d790e9",
"id": "aa@aa.com",
"intro": null,
"name": "aaa",
"eventList": [
{
"eventId": "5a2e18fa787af15faad72e5b",
"title": "abc",
"joinDate": "2018-01-12T00:17:33+09:00",
"point": 0
},
{
"eventId": "5a5486b9067610a13c4d401b",
"title": "bcd",
"joinDate": "2018-01-12T12:15:01+09:00",
"point": 100
}
]
},
{
"_id": "5a5719520d76a14727a0c709",
"id": "bbb@bbb.com",
"intro": null,
"name": "bbb",
"eventList": [
{
"eventId": "5a2e18fa787af15faad72e5b",
"title": "abc",
"joinDate": "2018-01-11T16:59:16+09:00",
"point": 12
}
]
},
{
"_id": "5a57193f99e37347212c33f8",
"id": "ccc@ccc.com",
"name" : "ccc",
"eventList": [
{
"eventId": "5a2e18fa787af15faad72e5b",
"title": "abc",
"joinDate": "2018-01-11T16:58:57+09:00",
"point": 3
}
]
}
]
我想按userList数组中标题为“abc”的“点”值对这些文档进行排序。
我应该如何为此构建查询
我对此提出质疑
User.find({"eventList.eventId" : {eventId}}).sort({"eventList.point":-1})
结果是a-b-c
但我认为应该是b-c-a
我该如何解决这个问题;(
谢谢..使用我们可以做到这一点
在事件列表数组中展开文档的步骤
查找匹配的文档
db.collection_name.aggregate([
{$unwind:"$eventList"},
{$match:{"eventList.title":"abc"}},
{$sort:{"eventList.point":-1}}
]);
整理文件
db.collection_name.aggregate([
{$unwind:"$eventList"},
{$match:{"eventList.title":"abc"}},
{$sort:{"eventList.point":-1}}
]);
对于按升序排列的文档,使用1;对于按降序排列的文档,使用-1我对这个用户进行了查询。查找({“eventList.eventId”:{eventId})。排序({“eventList.point”:-1})结果是a-b-c,但我认为应该是b-c-aThus,因为eventList数组中有多个文档,所以使用聚合管道来实现result@skylerByun-请使用聚合管道查看修改后的答案