MongoDB / Pymongo查询与日期时间 [英] MongoDB / Pymongo Query with Datetime

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

问题描述



我的代码是在下面(在通过正则表达式解析之后)

 如果连接不是无:
db.model.insert({time:datetime.datetime(int(int3),int (int1),
int(int2),int(int4),
int(int5),int(int6),
int(int7))})

然后在shell中输入两个数据点。

 >>> start = datetime.datetime(2012,2,2,6,35,6,764)
>>> end = datetime.datetime(2012,2,2,6,55,3,381)

然后尝试查询这两个数据点之间的数据范围,并收到返回的数据。

 >>> db.wing_model.find({'time':{'$ gte':start,'$ lt':end}})
< pymongo.cursor.Cursor对象在0x0301CFD0>
>>> db.wing_model.find({'time':{'$ gte':start,'$ lt':end}})
< pymongo.cursor.Cursor对象在0x0301C110>

数据列在下面。

  [02/02/2012 06:32:07.334] [INFO] 
[02/02/2012 06:32:07.334] [INFO]
[02/02 / 2012 06:32:07.334] [INFO]
[02/02/2012 06:32:13.711] [INFO]
[02/02/2012 06:32:13.711] [INFO]
[02/02/2012 06:32:13.711] [INFO]
[02/02/2012 06:32:22.473] [INFO]
[02/02/2012 06:32 :22.473] [INFO]
[02/02/2012 06:32:22.473] [INFO]
[02/02/2012 06:35:06.764] [INFO]
[02 / 02/2012 06:35:06.765] [INFO]
[02/02/2012 06:35:06.765] [INFO]
[02/02/2012 06:54:52.008] [INFO ]
[02/02/2012 06:54:52.008] [INFO]
[02/02/2012 06:54:52.008] [INFO]
[02/02/2012 06 :54:59.512] [INFO]
[02/02/2012 06:54:59.512] [INFO]
[02/02/2012 06:54:59.512] [INFO]
[02/02/2012 06:55:03.381] [INFO]
[02/02/2012 06:55:03.381] [INFO]
[02/02/2012 06:55:03.381] [INFO]
[02/02/2012 06:55:06.142] [INFO]
[02/02/2012 06:55:06.142] [INFO]
[02/02 / 2012 06:55:06.142] [INFO]
[02/02/2012 06:55:09.652] [INF O]
[02/02/2012 06:55:09.652] [INFO]
[02/02/2012 06:55:09.652] [INFO]
[02/02/2012 06:55:13.396] [INFO]
[02/02/2012 06:55:13.396] [INFO]

如何让我的查询返回开始和结束数据之间的所有内容?



此外,我如何以可理解的形式收到?



最后,为什么同一个查询返回不同的光标对象位置?



谢谢。

解决方案

基本教程文档:

  start = datetime.datetime(2012,2,2,6,35,6,764)
end = datetime.datetime(2012,2,2,6,55,3,381)

在db.wing_model.find中的文档({'time':{'$ gte'开始,'$ lt':end}}):
打印doc




最后,为什么同一个查询返回不同的游标对象
位置?


应该在哪里?



您会看到两个不同的游标实例,可能会返回相同的结果集 - 或?


I am trying to retrieve the data I have inserted into mongodb via pymongo.

My code for insert is below (after parsing via regex)

if connection is not None:
    db.model.insert({"time": datetime.datetime(int(int3), int(int1),
                                               int(int2), int(int4),
                                               int(int5), int(int6),
                                               int(int7))})

I then entered two data points in the shell.

>>> start = datetime.datetime(2012, 2, 2, 6, 35, 6, 764)
>>> end = datetime.datetime(2012, 2, 2, 6, 55, 3, 381)

I then tried to query the range of data between these two data points and received what is returned.

>>> db.wing_model.find({'time': {'$gte': start, '$lt': end}})
<pymongo.cursor.Cursor object at 0x0301CFD0>
>>> db.wing_model.find({'time': {'$gte': start, '$lt': end}})
<pymongo.cursor.Cursor object at 0x0301C110>

Data is listed below.

[02/02/2012 06:32:07.334][INFO]
[02/02/2012 06:32:07.334][INFO]
[02/02/2012 06:32:07.334][INFO]
[02/02/2012 06:32:13.711][INFO]
[02/02/2012 06:32:13.711][INFO]
[02/02/2012 06:32:13.711][INFO]
[02/02/2012 06:32:22.473][INFO]
[02/02/2012 06:32:22.473][INFO]
[02/02/2012 06:32:22.473][INFO]
[02/02/2012 06:35:06.764][INFO]
[02/02/2012 06:35:06.765][INFO]
[02/02/2012 06:35:06.765][INFO]
[02/02/2012 06:54:52.008][INFO]
[02/02/2012 06:54:52.008][INFO]
[02/02/2012 06:54:52.008][INFO]
[02/02/2012 06:54:59.512][INFO]
[02/02/2012 06:54:59.512][INFO]
[02/02/2012 06:54:59.512][INFO]
[02/02/2012 06:55:03.381][INFO]
[02/02/2012 06:55:03.381][INFO]
[02/02/2012 06:55:03.381][INFO]
[02/02/2012 06:55:06.142][INFO]
[02/02/2012 06:55:06.142][INFO]
[02/02/2012 06:55:06.142][INFO]
[02/02/2012 06:55:09.652][INFO]
[02/02/2012 06:55:09.652][INFO]
[02/02/2012 06:55:09.652][INFO]
[02/02/2012 06:55:13.396][INFO]
[02/02/2012 06:55:13.396][INFO]

How do I get my query to return everything between the 'start' and 'end' data?

Also, how do I receive this in an intelligible form?

Finally, why does the same query return different cursor object locations?

Thank you.

解决方案

Repeating existing basic tutorial documentation:

start = datetime.datetime(2012, 2, 2, 6, 35, 6, 764)
end = datetime.datetime(2012, 2, 2, 6, 55, 3, 381)

for doc in db.wing_model.find({'time': {'$gte': start, '$lt': end}}):
    print doc

Finally, why does the same query return different cursor object locations?

Where should that be the case?

You see two different cursor instances which will likely return the same result set - or?

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

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