Mongoose 查询帮助:嵌套数组的分页、排序、限制

Mongoose Query Help: Paginate, Sort, Limit on Nested Array

我有一个聊天Mongoose模型,下面是示例数据。如果这仍然不清楚,请将您的问题回复给我。非常感谢任何帮助。

        { 
            "_id" : ObjectId("5745910831a1sd58d070a8faa"), 
            "messages" : [
                {
                    "user" : "user1", 
                    "message" : "How are you user1?", 
                    "readInd" : "N", 
                    "createDate" : ISODate("2016-05-25T11:36:00.468+0000"), 
                    "_id" : ObjectId("5745912c31a1c58d070a904d")
                }, 
                {
                    "user" : "user1", 
                    "message" : "Hello user1", 
                    "readInd" : "N", 
                    "createDate" : ISODate("2016-05-25T11:38:53.893+0000"), 
                    "_id" : ObjectId("5745912531a1c58d070a902e")
                }
            ], 
            "createDate" : ISODate("2016-05-25T11:35:20.534+0000"), 
            "users" : [
                "57450b4506561ff5052f0a66", 
                "57450d8108d8d22c06cf138f"
            ], 
            "__v" : NumberInt(0)
        },
        { 
            "_id" : ObjectId("57458e9331a1c58d070a8e30"), 
            "messages" : [
                {
                    "user" : "user2", 
                    "message" : "How are you user2", 
                    "readInd" : "N", 
                    "createDate" : ISODate("2016-05-25T11:46:03.240+0000"), 
                    "_id" : ObjectId("574590f331a1c58d070a8ede")
                }, 
                {
                    "user" : "user2", 
                    "message" : "Hello user2", 
                    "readInd" : "N", 
                    "createDate" : ISODate("2016-05-25T11:48:53.925+0000"), 
                    "_id" : ObjectId("574590e931a1c58d070a8eab")
                }
            ], 
            "createDate" : ISODate("2016-05-25T11:35:20.534+0000"), 
            "users" : [
                "5745149e3aaab38706c00b64", 
                "57450d8108d8d22c06cf138f"
            ], 
            "__v" : NumberInt(0)
        }
        { 
            "_id" : ObjectId("5745910831a1c58d070a8faa"), 
            "messages" : [
                {
                    "user" : "user3", 
                    "message" : "How are you user3?", 
                    "readInd" : "N", 
                    "createDate" : ISODate("2016-05-25T11:56:00.468+0000"), 
                    "_id" : ObjectId("5745912c31a1c58d070a904d")
                }, 
                {
                    "user" : "user3", 
                    "message" : "Hello user3", 
                    "readInd" : "N", 
                    "createDate" : ISODate("2016-05-25T11:58:53.893+0000"), 
                    "_id" : ObjectId("5745912531a1c58d070a902e")
                }
            ], 
            "createDate" : ISODate("2016-05-25T11:35:20.534+0000"), 
            "users" : [
                "57450b4506561ff5052f0a66", 
                "57450d8108d8d22c06cf138f"
            ], 
            "__v" : NumberInt(0)
        },
        { 
            "_id" : ObjectId("5745910831a1c58d070a8faa"), 
            "messages" : [
                {
                    "user" : "user4", 
                    "message" : "How are you user4?", 
                    "readInd" : "N", 
                    "createDate" : ISODate("2016-05-25T11:66:00.468+0000"), 
                    "_id" : ObjectId("5745912c31a1c58d070a904d")
                }, 
                {
                    "user" : "user4", 
                    "message" : "Hello user4", 
                    "readInd" : "N", 
                    "createDate" : ISODate("2016-05-25T11:68:53.893+0000"), 
                    "_id" : ObjectId("5745912531a1c58d070a902e")
                }
            ], 
            "createDate" : ISODate("2016-05-25T11:35:20.534+0000"), 
            "users" : [
                "57450b4506561ff5052f0a66", 
                "57450d8108d8d22c06cf138f"
            ], 
            "__v" : NumberInt(0)
        }

下面是解释:

  1. 用户 1 分别在 11:36 和 11:38 发送了 2 条消息
  2. user2 分别在 11:46 和 11:48 发送了 2 条消息
  3. 用户 3 分别在 11:56 和 11:58 发送了 2 条消息
  4. user4 分别在 11:66 和 11:68 发送了 2 条消息

我的预期结果是:

Pagination/limit 条件:

  1. 每页显示 2 条记录。
  2. 仅显示基于用户的最新消息。

示例输出:

第1页:

   "user" : "57450d8108d8d22c06cf138f",              
   "message" : "How are you user4?"

   "user" : "57450d8108d8d22c06cf138f", 
   "message" : "How are you user3?"

第2页:

   "user" : "57450d8108d8d22c06cf138f", 
   "message" : "How are you user2"

   "user" : "57450d8108d8d22c06cf138f", 
   "message" : "How are you user1?"

试试这种查询方式,对你有帮助

查询第一页

db.getCollection('message').aggregate( [ { $match : { user : "57450d8108d8d22c06cf138f" } },
{ $unwind : "$messages" } ,
{ $sort : { 'messages.createDate' : -1} },
 { $limit : 2 },
 { $project : { _id: 0,'message':'$messages.message','user':'$messages.user'} } ])

查询下一页

db.getCollection('message').aggregate( [ { $match : { user : "57450d8108d8d22c06cf138f" } },
    { $unwind : "$messages" } ,
    { $sort : { 'messages.createDate' : -1} },
     { $limit : 2 },{ $skip : 2 }
     { $project : { _id: 0,'message':'$messages.message','user':'$messages.user'} } ])