文档仅将非空列保存到文档中时出现的排序问题: 我的mongodb文件看起来像:
# documents-1 :
"taskId": "2",
"taskType": "RFOUTAUD",
"id": "5d25bdda2f7ef0000654f281",
"releaseDatetime": "2019-05-28T09:00:39",
"startDateTime": "2019-05-28T09:44:11",
"endDateTime": "2019-05-28T09:50:25",
"resourceId": "SUPER",
"deviceCode": "QATERM1",
"effectivePriority": 40,
"sourceLocation": "1SS100",
"sourceLocationTravelSequence": "020055000",
"sourceWorkZone": "1RCVSHP035",
"sourceWorkZoneTravelSequence": "15035",
"taskId": "70",
"taskType": "RFOUTAUD",
"id": "5d25bdda2f7ef0000654f281",
"releaseDatetime": "2019-05-28T09:00:39",
"startDateTime": "2019-05-28T09:44:11",
"endDateTime": "2019-05-28T09:50:25",
"resourceId": "SUPER",
"deviceCode": "QATERM1",
"effectivePriority": 40,
"sourceLocation": "1SS100",
"sourceLocationTravelSequence": "020055000",
"sourceWorkZone": "1RCVSHP035",
"goalTime": 20
#documents-1的记录数为n:#documents-2的记录数很少-具有附加列“ goalTime”
现在,当我在“ goalTime”列上使用Pageable进行排序时,结果是我仅获得具有“ goalTime”的文档,并且排除了缺少该列的文档 查询看起来像这样
db.getCollection('29ec09db-20b9-4c52-a5af-53519f75a5b4_task').find({}).limit(10).sort({goalTime:1})
在完成org.springframework.data.domain.Sort实现之后,我找到了一种方法来包括那些缺少列本身的文档。 现在的问题是文档的“ goalTime”列的值作为最后一条记录出现,如果我将限制10放入,则该结果将不属于该结果。
这是mongo查询:
`db.getCollection('29ec09db-20b9-4c52-a5af-53519f75a5b4_task').find({}).limit(2).sort({goalTime:1,NULLS_LAST:1})`
我正在寻找结果,该结果首先显示按值对列进行排序的文档,然后显示缺少列的文档
答案 0 :(得分:1)
我找到了所需的解决方案。即使文档db.getCollection('29ec09db-20b9-4c52-a5af-53519f75a5b4_task').find({}).limit(20).sort({goaltime: -1, NULL_LAST : 1})