gpt4 book ai didi

java - 按降序对数组进行排序并仅返回数组中匹配的元素

转载 作者:太空宇宙 更新时间:2023-11-04 09:13:50 25 4
gpt4 key购买 nike

/* 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")
}
}
]
}


我想按降序对 session 数组进行排序,然后应用分页以仅检索响应中匹配的 session ,而不是完整的数组。因此,对于第一页,我想请求 5 次 session ,那么只会返回/预计 5 次 session 。在第 2 页请求下 5 页等。目前我得到的是一系列完整的 session 。这不是我想要的。

最佳答案

尝试一下这个聚合查询:

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
}
]);

关于java - 按降序对数组进行排序并仅返回数组中匹配的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59343120/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com