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

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

问题描述

我正在使用 mongodb 并以这种方式将日期时间存储在我的数据库中

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

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

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

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})

有人知道怎么做这个查询吗?

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天全站免登陆