mongodb mongodb中的日期比较
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31071999/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Date comparison in mongodb
提问by maggs
I want to retrieve all the documents after a particular date.
My database has date as - DateAdded:"2014-12-17 10:03:46.000Z"
我想在特定日期后检索所有文档。我的数据库的日期为 -DateAdded:"2014-12-17 10:03:46.000Z"
I wrote the following query-
我写了以下查询-
db.collection.find({DateAdded:{"$lte":new Date("2015-06-17 10:03:46.000Z")}})
But the results doesn't fetches any record even though there are records for the dates upto 2015-06-24
.
但是即使有日期的记录 up to ,结果也不会获取任何记录2015-06-24
。
回答by L_7337
You can use ISODateto compare dates:
您可以使用ISODate来比较日期:
"$lte" : ISODate("2015-06-17T10:03:46Z")
ISODateworks because that is the format your date is in.
ISODate有效,因为这是您的日期格式。
new Date()
wraps the date in an ISODate helper, but is not able to convert in your query.
将日期包装在 ISODate 助手中,但无法在您的查询中进行转换。
Check out this link for more information: http://docs.mongodb.org/manual/core/shell-types/
查看此链接以获取更多信息:http: //docs.mongodb.org/manual/core/shell-types/
回答by Aljohn Yamaro
Use ISODate
使用ISODate
format:
格式:
{'field': {$operator: ISODate(yyyy-MM-ddThh:mm:ss.msZ)}}
example:
例子:
db.getCollection('yourCollection').find({'sampleField': {$lte: ISODate('2015-06-17T10:03:46.000Z')}})
回答by Abdul Rahman A Samad
You can use this:
你可以使用这个:
db.collection.find({DateAdded:{"$lte":new Date("2017-11-01")}})
It will convert your date format into this:
它会将您的日期格式转换为:
ISODate("2017-11-01T00:00:00Z")