gpt4 book ai didi

Mongodb聚合跳过和限制排序在分页中带来重复记录

转载 作者:行者123 更新时间:2023-12-05 08:32:17 24 4
gpt4 key购买 nike

我有以下查询,首先对文档进行排序,然后跳过并限制 10 条记录,以下是我的查询:

db.getCollection('jobpostings').aggregate([
{"$match":{
"expireDate":{"$gte": ISODate("2018-08-12T00:00:00.000Z")},
"publishDate":{"$lt": ISODate("2018-08-13T00:00:00.000Z")},
"isPublished":true,
"isDrafted":false,
"deletedAt":{"$eq":null},
"deleted":false,
"blocked":{"$exists":false}
}},
{"$lookup":{"from":"companies","localField":"company.id","foreignField":"_id","as":"companyDetails"}},
{"$match":{"companyDetails":{"$ne":[]}}},
{"$sort":{
"isFeatured":-1,
"refreshes.refreshAt":-1,
"publishDate":-1
}},
{"$skip":0},
{"$limit":10},
{"$project":{
"position":1,"summary":1,"company":1,"publishDate":1,
"expireDate":{"$dateToString":{"format":"%Y-%m-%d","date":"$expireDate"}},
"locations":1,"minimumEducation":1,"workType":1,"skills":1,"contractType":1,
"isExtensible":1,"salary":1,"gender":1,"yearsOfExperience":1,"canApplyOnline":1,"number":1,
"isFeatured":1,"viewsCount":1,
"status":{"$cond":{
"if":{"$and":[
{"$lt":["$publishDate", ISODate("2018-08-13T00:00:00.000Z")]},
{"$gt":["$publishDate", ISODate("2018-08-11T00:00:00.000Z")]}]},"then":"New",
"else":{"$cond":{
"if":{"$lt":["$publishDate",ISODate("2018-08-12T00:00:00.000Z")]},"then":"Old","else":"Future"}}}},
"companyDetails.profilePic":1,"companyDetails.businessUnits":1,"companyDetails.totalRatingAverage":1,
"expiringDuration":{"$floor":{"$divide":[{"$subtract":["$expireDate",ISODate("2018-08-12T00:00:00.000Z")]},
86400000]}},
"companyDetails.totalReviews":{"$size":{"$ifNull":[{"$let":{"vars":{
"companyDetailOne":{"$arrayElemAt":["$companyDetails",0]}},"in":"$$companyDetailOne.reviews"}},[]]}}}}

])

如果我评论 skip and limit 以下是我的结果: no limit

但以下是我在 skip = 0, limit = 10 时的结果: first page

现在将上面的结果与以下 skip=10, limit=10 的结果进行比较:突出显示的文档在第二页中重复 (skip=10, limit=10): second page

对于其他文档,其他页面中也存在同样的事情。

最佳答案

您排序所依据的三个字段似乎不是唯一的,因此在后续执行中顺序可能会有所不同。要解决此问题,您可以向 $sort 添加其他字段。由于 _id 始终是唯一的,因此它可能是一个很好的候选者。尝试:

{"$sort":{
"isFeatured":-1,
"refreshes.refreshAt":-1,
"publishDate":-1,
"_id": -1
}}

关于Mongodb聚合跳过和限制排序在分页中带来重复记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51805886/

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