我在mongo shell中执行此查询
db.getCollection('list').find({}).sort({next_time: 1})
Run Code Online (Sandbox Code Playgroud)
结果是
next_time
--
null
null
null
2015-02-21 00:00:00
2015-03-25 00:00:00
2015-08-29 00:00:00
Run Code Online (Sandbox Code Playgroud)
我希望得到这样的结果
next_time
--
2015-02-21 01:00:00
2015-03-25 01:00:00
2015-08-29 01:00:00
null
null
null
Run Code Online (Sandbox Code Playgroud)
不同的是'null'是最后在列表中排序的.我怎么能这样做?
也许您可以使用聚合和人为的高结束日期:
c = db.foo.aggregate([
{$project: {
next_time: 1,
nlt: { $ifNull: [ "$next_time", new ISODate("9000-01-01") ] }
}
}
,
{$sort: { "nlt": 1}}
]);
c.forEach(function(r) { printjson(r); });
Run Code Online (Sandbox Code Playgroud)
或者,如果大多数材料都有空,而您根本不想处理这些文档,那么将它们过滤掉,只剩$sort下剩下的:
db.foo.aggregate([
{$match: {"nt": {$exists: true}}}
,
{$sort: { "nt": 1}}
]);
Run Code Online (Sandbox Code Playgroud)
如果您可以向文档添加字段,则可以添加一个“反向”字段,当您按降序排序时,该字段会按升序返回您的文档。
只是为了澄清:mongo 将 null 值视为“最小”值,因此它们在按升序排序时首先出现,在按降序排序时最后出现。
所以,如果你的元素是这样的:
{
...
next_time: ISODate("2020-05-01")
}
Run Code Online (Sandbox Code Playgroud)
既然ISODate("2020-05-01").getTime()是1588291200000,您可以添加:
{
...
next_time: ISODate("2020-05-01"),
next_time_inverted: -1588291200000
}
Run Code Online (Sandbox Code Playgroud)
然后将反转后的值按降序排序:
db.getCollection('list').find({}).sort({next_time_inverted: -1})
Run Code Online (Sandbox Code Playgroud)
必要时不要忘记在该字段上添加索引。
| 归档时间: |
|
| 查看次数: |
1845 次 |
| 最近记录: |