MongoDB找到与CurrentDate的Query比较(MongoDB find Query comparision with CurrentDate)

系统教程 行业动态 更新时间:2024-06-14 16:57:40
MongoDB找到与CurrentDate的Query比较(MongoDB find Query comparision with CurrentDate)

我想从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 } }

I want to fetch current day documents from a MongoDB collection. My documents look like this:

{ "_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 } }

最满意答案

根据您的问题, 您想从mongodb集合中获取当前日期文档 。 在mongoDB shell中,当你键入new Date()它会给你当前的日期和时间,当你运行相同的new Date()时,这个值总是变化的,所以你的查询可能是这样的:

db.collectionName.find({"start_date":new Date()}).pretty()

但是,我认为此查询会返回将在您的集合中显示的那些文档,但您的文档中可能不会显示相同的当前Date值。所以这种情况下您应该使用以下

db.collectionName.find({"start_date":{"$lte":new Date()}}).pretty()

要么

db.collectionName.find({"start_date":{"$gte":new Date()}}).pretty()

在某些情况下如果你想找到与year,month,day完全匹配year,month,day那么你应该在$ project中使用$ year,$ month,$ 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() } })

在上面的聚合查询中将返回与当前日期匹配的文档,如当前日期的year,month,day 。 你也将$match替换为

var currentDate = new Date() { "$match": { "year": currentDate.getFullYear(), "month": currentDate.getMonth() + 1, //because January starts with 0 "day": currentDate.getDate() } }

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 :

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

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 aggregation with $year,$month,$dayOfMonth in $project like this :

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

var currentDate = new Date() { "$match": { "year": currentDate.getFullYear(), "month": currentDate.getMonth() + 1, //because January starts with 0 "day": currentDate.getDate() } }

更多推荐

MongoDB,_id,ObjectId,documents,电脑培训,计算机培训,IT培训"/> <meta name=&quo

本文发布于:2023-04-13 12:41:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/650161e94a085e04089264370bd0fd5e.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:CurrentDate   MongoDB   Query   comparision   find

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!