MongoDB 查找查询与 CurrentDate 的比较
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30772285/
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
MongoDB find Query comparision with CurrentDate
提问by Saikumar A
I want to fetch current day documents from a MongoDB collection. My documents look like this:
我想从 MongoDB 集合中获取当天的文档。我的文件是这样的:
{
"_id" : ObjectId("55743941789a9abe7f4af3fd"),
"msisdn" : "9xxxxxxxxxx",
"act_date" : ISODate("2014-11-24T00:00:00Z"),
"date" : ISODate("2015-06-07T00:00:00Z"),
"recharge" : { "recharge_amt" : 0, "rechargetype" : "WEB" },
"voice" : { "local_og_mou" : 20, "local_other_mobile_og_mou" : 0, "nld_og_mou" : 0, "nld_other_mobile_og_mou" : 10 },
"gprs" : { "total_access_count" : 1, "total_datavolume_mb" : 42 },
"sms" : { "freesms" : 3, "local_sms_count" : 0, "nat_sms_count" : 0, "inter_sms_count" : 0 }
}
回答by Yogesh
As per your question you want to fetch current day documents from a mongodb collection. In mongoDB shell when you type new Date()
it gives you current date with time and this value always vary when you run same new Date()
so probably your query like this :
根据您的问题,您想从 mongodb 集合中获取当天的文档。在 mongoDB shell 中,当您键入new Date()
它时,它会为您提供当前日期和时间,并且当您运行相同时,该值总是会有所不同,new Date()
因此您的查询可能如下所示:
db.collectionName.find({"start_date":new Date()}).pretty()
But, I think this query return the those documents which will presents in your collection but same current Date
value may be not presents in your documents SO this case you should use following
但是,我认为此查询返回将出现在您的集合中的那些文档,但Date
您的文档中可能不存在相同的当前值,因此在这种情况下,您应该使用以下内容
db.collectionName.find({"start_date":{"$lte":new Date()}}).pretty()
Or
或者
db.collectionName.find({"start_date":{"$gte":new Date()}}).pretty()
In some case If you want to find exact match with year,month,day
then you should use aggregationwith $year,$month,$dayOfMonth in $projectlike this :
在某些情况下,如果你想找到精确匹配year,month,day
,那么你应该使用聚合与$年,$月份,$请将dayOfMonth在$项目是这样的:
db.collectionName.aggregate({
"$project": {
"year": {
"$year": "$date"
},
"month": {
"$month": "$date"
},
"day": {
"$dayOfMonth": "$date"
}
}
}, {
"$match": {
"year": new Date().getFullYear(),
"month": new Date().getMonth() + 1, //because January starts with 0
"day": new Date().getDate()
}
})
In above aggregation query will return those documents which match current date like year,month,day
of current date. Also you replace $match
as
在上面的聚合查询中,将返回那些与当前日期匹配的文档,例如year,month,day
当前日期。你也替换$match
为
var currentDate = new Date()
{
"$match": {
"year": currentDate.getFullYear(),
"month": currentDate.getMonth() + 1, //because January starts with 0
"day": currentDate.getDate()
}
}
回答by Nikhil Bhandarkar
Please include timeStamp: true
after your schema definition to get autogenerated createdAt and updatedAt fields MongoDB will take care of it.
请timeStamp: true
在您的架构定义之后包含以获取自动生成的 createdAt 和 updatedAt 字段,MongoDB 会处理它。
const itemSchema = mongoose.Schema({
// Your Schema definition here
}, {
timestamps: true
})
In your case, you need to compare your TimeStamp key with the start of today which is 12 AMwith ISO string as 2019-11-08T00:00:00.000Z
and end of the day which is 11:59 PMwith ISO string as 2019-11-08T23:59:59.999Z
.
The below code will do it for you.
在您的情况下,您需要将您的 TimeStamp 密钥与今天的开始时间进行比较,即今天上午 12 点,ISO 字符串为2019-11-08T00:00:00.000Z
,当天结束时间为晚上 11:59,ISO 字符串为2019-11-08T23:59:59.999Z
。
下面的代码将为您完成。
let queryObj = {}
const startOfDay = new Date(new Date().setUTCHours(0, 0, 0, 0)).toISOString()
const endOfDay = new Date(new Date().setUTCHours(23, 59, 59, 999)).toISOString()
queryObj.createdAt = {
$gte: startOfDay, // 2019-11-08T00:00:00.000Z
$lt: endOfDay // 2019-11-08T23:59:59.999Z
}
let items = item.find(obj)
// new Date().setUTCHours(0, 0, 0, 0) Generates this weird string '1573171200000' which is not a human readable format of TimeStamp
// To convert it into ISO String we have .toISOString() method
回答by Dipesh Parmar
You can do this using below query.
您可以使用以下查询执行此操作。
db.collection.find({"start_date" : { $gte : new ISODate("2015-05-27T00:00:00Z") }});
Note : above query will return documents which are greather than specified date.
注意:上面的查询将返回大于指定日期的文档。
To find a date that equals another date
查找与另一个日期相等的日期
db.collection.find({"start_date" : new ISODate("2015-05-27T00:00:00Z") });