如何处理在Mongodb中存储为字符串的日期? [英] How to handle date stored as string in Mongodb?
问题描述
因此,我有一个名为 task_time
的字段,以字符串形式以'YYYY-MM-DD'
格式(例如'2012-12-21')存储在Mongodb中.
So I have this field called task_time
stored as string in Mongodb in 'YYYY-MM-DD'
format (eg. '2012-12-21').
现在,我需要查询此集合以获取其 task_time
在给定时间间隔内的数据.
Now I need to query this collection to get the data whose task_time
is within a given time interval.
时间间隔以一对字符串形式给出,以'YYYY-MM-DD hh:mm:ss'
格式表示开始时间和结束时间(例如'2015-12-2116:00:00').
The time interval is given as a pair of strings that represent start time and end time in 'YYYY-MM-DD hh:mm:ss'
format (eg. '2015-12-21 16:00:00').
是否有任何方法可以直接在Mongo查询中执行而无需将此任务带到我的JavaScript代码中?
Is there any way to do this directly in Mongo query without bringing this task to my javascript code?
推荐答案
据我所知, $ gte
和 $ lt
也可以使用字符串.此问题之后
As I know $gte
and $lt
can work with strings too. The same in mongoose after this issue
items.find({
task_time: {
$gte: "2015-12-21 12:00:00",
$lt: "2015-12-21 16:00:00"
}
})
这篇关于如何处理在Mongodb中存储为字符串的日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!