MongoDB排序日期字符串(mm/dd/yyyy) [英] MongoDB sorting date string (mm/dd/yyyy)
本文介绍了MongoDB排序日期字符串(mm/dd/yyyy)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我以"mm/dd/yyyy"格式将日期存储为字符串.我想按此日期字段排序.我在下面的查询中尝试了一些测试数据.
I'm storing date as string in "mm/dd/yyyy" format. I want to sort by this date field. I tried below query with few test data.
db.collection.find().sort({date: -1}).pretty()
,它工作正常.可以正常工作吗?还是应该将其转换为MongoDate以进行可靠的排序?
and it is working fine. Would this work fine ever or should i convert this to MongoDate for reliable sorting?
推荐答案
以yyyy
为最后,这种分类将无法使用多年.
With yyyy
being last, that sort isn't going to work across years.
最好切换到yyyy-mm-dd
格式的字符串或实际的Date
类型.两者都将正确排序.
Probably best to switch to a yyyy-mm-dd
formatted string or an actual Date
type. Both of those will sort correctly.
这篇关于MongoDB排序日期字符串(mm/dd/yyyy)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文