按降序对数组排序,并从数组中仅返回匹配的elemnet

时间:2019-12-15 10:45:54

标签: java mongodb mongodb-query spring-mongodb

/* 1 */
{
    "_id" : ObjectId("5c4ff961d4404a0dccec4f71"),
    "tier" : "3",
    "agentName" : "Dial2Travel1",
    "meeting" : [ 
        {
            "meetingDate" : ISODate("2019-01-23T00:00:00.000Z"),
            "comment" : "call back after 4 days",
            "meetingDoneBy" : {
                "$ref" : "salesExecutive",
                "$id" : ObjectId("5c6b9dc9d56dbad67a951786")
            }
        }, 
        {
            "meetingDate" : ISODate("2019-02-23T00:00:00.000Z"),
            "comment" : "wrong number",
            "meetingDoneBy" : {
                "$ref" : "salesExecutive",
                "$id" : ObjectId("5c6b9dc9d56dbad67a951786")
            }
        }, 
        {
            "meetingDate" : ISODate("2019-03-12T00:00:00.000Z"),
            "comment" : "packages request",
            "meetingDoneBy" : {
                "$ref" : "salesExecutive",
                "$id" : ObjectId("5c6b9dc9d56dbad67a951786")
            }
        }
    ]
}

/* 2 */
{
    "_id" : ObjectId("5c4ff961d4404a0dccec4f72"),
    "tier" : "3",
    "agentName" : "Dial2Travel2",
    "meeting" : [ 
        {
            "meetingDate" : ISODate("2019-01-23T00:00:00.000Z"),
            "comment" : "call back after 4 days",
            "meetingDoneBy" : {
                "$ref" : "salesExecutive",
                "$id" : ObjectId("5c6b9dc9d56dbad67a951786")
            }
        }, 
        {
            "meetingDate" : ISODate("2019-02-23T00:00:00.000Z"),
            "comment" : "wrong number",
            "meetingDoneBy" : {
                "$ref" : "salesExecutive",
                "$id" : ObjectId("5c6b9dc9d56dbad67a951786")
            }
        }, 
        {
            "meetingDate" : ISODate("2019-03-12T00:00:00.000Z"),
            "comment" : "packages request",
            "meetingDoneBy" : {
                "$ref" : "salesExecutive",
                "$id" : ObjectId("5c6b9dc9d56dbad67a951786")
            }
        }
    ]
}

/* 3 */
{
    "_id" : ObjectId("5c4ff961d4404a0dccec4f73"),
    "tier" : "3",
    "agentName" : "Dial2Travel3-",
    "meeting" : [ 
        {
            "meetingDate" : ISODate("2019-01-23T00:00:00.000Z"),
            "comment" : "call back after 4 days",
            "meetingDoneBy" : {
                "$ref" : "salesExecutive",
                "$id" : ObjectId("5c6b9dc9d56dbad67a951786")
            }
        }, 
        {
            "meetingDate" : ISODate("2019-02-23T00:00:00.000Z"),
            "comment" : "wrong number",
            "meetingDoneBy" : {
                "$ref" : "salesExecutive",
                "$id" : ObjectId("5c6b9dc9d56dbad67a951786")
            }
        }, 
        {
            "meetingDate" : ISODate("2019-03-12T00:00:00.000Z"),
            "comment" : "packages request",
            "meetingDoneBy" : {
                "$ref" : "salesExecutive",
                "$id" : ObjectId("5c6b9dc9d56dbad67a951786")
            }
        }
    ]
}

/* 4 */
{
    "_id" : ObjectId("5c4ff961d4404a0dccec4f74"),
    "tier" : "3",
    "agentName" : "Dial2Travel4",
    "meeting" : [ 
        {
            "meetingDate" : ISODate("2019-01-23T00:00:00.000Z"),
            "comment" : "call back after 4 days",
            "meetingDoneBy" : {
                "$ref" : "salesExecutive",
                "$id" : ObjectId("5c6b9dc9d56dbad67a951786")
            }
        }, 
        {
            "meetingDate" : ISODate("2019-02-23T00:00:00.000Z"),
            "comment" : "wrong number",
            "meetingDoneBy" : {
                "$ref" : "salesExecutive",
                "$id" : ObjectId("5c6b9dc9d56dbad67a951786")
            }
        }, 
        {
            "meetingDate" : ISODate("2019-03-12T00:00:00.000Z"),
            "comment" : "packages request",
            "meetingDoneBy" : {
                "$ref" : "salesExecutive",
                "$id" : ObjectId("5c6b9dc9d56dbad67a951786")
            }
        }
    ]
}


我想按降序对会议数组进行排序,然后应用分页以仅在响应中检索匹配的会议,而不是完整的数组。 因此,对于第一页,我想请求5次会议,那么仅5次会议将被退回/计划。在第二页上请求下一个5,依此类推。 目前我正在开会的是一整套会议。这不是我想要的。

1 个答案:

答案 0 :(得分:0)

尝试对此聚合查询:

var pageNum = 1;
var resPerPage = 5;
var skipAmt = pageNum * resPerPage;

db.Test.aggregate([
    {
        $unwind: "$meeting"
    },
    {
        $project: {
            "_id": 0,
            "meeting": "$meeting"
        }
    },
    {
        $sort: {
            "meeting.meetingDate": - 1
        }
    },
    {
        $skip: skipAmt
    },
    {
        $limit: resPerPage
    }
]);