查询Mongodb在月,日,年...的日期时间 [英] Query Mongodb on month, day, year... of a datetime

查看:267
本文介绍了查询Mongodb在月,日,年...的日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用mongodb,我以这种方式将datetime存储在我的数据库中



的日期17-11-2011 18:00我存储:

  date = datetime.datetime(2011,11,17,18,0)
db.mydatabase.mycollection.insert ({date:date})

我想要这样一个请求

  month = 11 
db.mydatabase.mycollection.find({date.month:month})

  day = 17 
db.mydatabase.mycollection.find({date.day:day})


$ b $任何人都知道如何执行此查询?

解决方案

日期以其时间戳格式存储。如果您想要属于特定月份的所有内容,请查询本月的开始和结束。

  var start = new日期(2010,11,1); 
var end = new Date(2010,11,30);

db.posts.find({created_on:{$ gte:start,$ lt:end}});
//取自http://cookbook.mongodb.org/patterns/date_range/


I'm using mongodb and I store datetime in my database in this way

for a date "17-11-2011 18:00" I store:

date = datetime.datetime(2011, 11, 17, 18, 0)
db.mydatabase.mycollection.insert({"date" : date})

I would like to do a request like that

month = 11
db.mydatabase.mycollection.find({"date.month" : month})

or

day = 17
db.mydatabase.mycollection.find({"date.day" : day})

anyone knows how to do this query?

解决方案

Dates are stored in their timestamp format. If you want everything that belongs to a specific month, query for the start and the end of the month.

var start = new Date(2010, 11, 1);
var end = new Date(2010, 11, 30);

db.posts.find({created_on: {$gte: start, $lt: end}});
//taken from http://cookbook.mongodb.org/patterns/date_range/

这篇关于查询Mongodb在月,日,年...的日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆