mongodb的日期时间的插入及查询

大家一定要注意下时间,时间要指定注入,别用mongodb自带的 new date了,他的时间是utc的时间,和咱们中国时区少8个小时。

当然你要是不觉得麻烦的话,可用python的pymongo库做一些时间的调整,但是还是麻烦。。。。  还是插入指定的时间把。

> use xiaorui
switched to db xiaorui
> 
> db.xiaorui.test.insert({'good':1})
> db.xiaorui.test.insert({'good':2})
> db.xiaorui.test.insert({'good':3})
> db.xiaorui.test.find({})
{ "_id" : ObjectId("536232595a87a26d8f721f9b"), "good" : 1 }
{ "_id" : ObjectId("5362325c5a87a26d8f721f9c"), "good" : 2 }
{ "_id" : ObjectId("5362325e5a87a26d8f721f9d"), "good" : 3 }
> 
> 
> db.xiaorui.test.insert({'ltime':ISODate("2012-11-02 07:58:51")})
> 
> db.xiaorui.test.find({})
{ "_id" : ObjectId("536232595a87a26d8f721f9b"), "good" : 1 }
{ "_id" : ObjectId("5362325c5a87a26d8f721f9c"), "good" : 2 }
{ "_id" : ObjectId("5362325e5a87a26d8f721f9d"), "good" : 3 }
{ "_id" : ObjectId("536232c15a87a26d8f721f9e"), "ltime" : ISODate("2012-11-02T07:58:51Z") }
> 
> db.xiaorui.test.insert({'ltime':ISODate("2012-11-02 07:58:55")})
> db.xiaorui.test.find({})
{ "_id" : ObjectId("536232595a87a26d8f721f9b"), "good" : 1 }
{ "_id" : ObjectId("5362325c5a87a26d8f721f9c"), "good" : 2 }
{ "_id" : ObjectId("5362325e5a87a26d8f721f9d"), "good" : 3 }
{ "_id" : ObjectId("536232c15a87a26d8f721f9e"), "ltime" : ISODate("2012-11-02T07:58:51Z") }
{ "_id" : ObjectId("536232cf5a87a26d8f721f9f"), "ltime" : ISODate("2012-11-02T07:58:55Z") }
> 
> 
> db.xiaorui.test.insert({'ltime':ISODate("2012-11-03 07:58:55")})
> db.xiaorui.test.insert({'ltime':ISODate("2012-11-04 07:58:55")})
> db.xiaorui.test.insert({'ltime':ISODate("2012-11-05 07:58:55")})
> db.xiaorui.test.insert({'ltime':ISODate("2012-11-06 07:58:55")})
> db.xiaorui.test.find({})
{ "_id" : ObjectId("536232595a87a26d8f721f9b"), "good" : 1 }
{ "_id" : ObjectId("5362325c5a87a26d8f721f9c"), "good" : 2 }
{ "_id" : ObjectId("5362325e5a87a26d8f721f9d"), "good" : 3 }
{ "_id" : ObjectId("536232c15a87a26d8f721f9e"), "ltime" : ISODate("2012-11-02T07:58:51Z") }
{ "_id" : ObjectId("536232cf5a87a26d8f721f9f"), "ltime" : ISODate("2012-11-02T07:58:55Z") }
{ "_id" : ObjectId("536232e05a87a26d8f721fa0"), "ltime" : ISODate("2012-11-03T07:58:55Z") }
{ "_id" : ObjectId("536232e35a87a26d8f721fa1"), "ltime" : ISODate("2012-11-04T07:58:55Z") }
{ "_id" : ObjectId("536232e65a87a26d8f721fa2"), "ltime" : ISODate("2012-11-05T07:58:55Z") }
{ "_id" : ObjectId("536232e85a87a26d8f721fa3"), "ltime" : ISODate("2012-11-06T07:58:55Z") }
> 
> 
> db.xiaorui.test.insert({'ltime':ISODate("2012-11-07 07:58:55")})
> db.xiaorui.test.insert({'ltime':ISODate("2012-11-08 07:58:55")})
> db.xiaorui.test.insert({'ltime':ISODate("2012-11-11 07:58:55")})
> 
> 
> 
> db.xiaorui.test.find({})
{ "_id" : ObjectId("536232595a87a26d8f721f9b"), "good" : 1 }
{ "_id" : ObjectId("5362325c5a87a26d8f721f9c"), "good" : 2 }
{ "_id" : ObjectId("5362325e5a87a26d8f721f9d"), "good" : 3 }
{ "_id" : ObjectId("536232c15a87a26d8f721f9e"), "ltime" : ISODate("2012-11-02T07:58:51Z") }
{ "_id" : ObjectId("536232cf5a87a26d8f721f9f"), "ltime" : ISODate("2012-11-02T07:58:55Z") }
{ "_id" : ObjectId("536232e05a87a26d8f721fa0"), "ltime" : ISODate("2012-11-03T07:58:55Z") }
{ "_id" : ObjectId("536232e35a87a26d8f721fa1"), "ltime" : ISODate("2012-11-04T07:58:55Z") }
{ "_id" : ObjectId("536232e65a87a26d8f721fa2"), "ltime" : ISODate("2012-11-05T07:58:55Z") }
{ "_id" : ObjectId("536232e85a87a26d8f721fa3"), "ltime" : ISODate("2012-11-06T07:58:55Z") }
{ "_id" : ObjectId("5362333b5a87a26d8f721fa4"), "ltime" : ISODate("2012-11-07T07:58:55Z") }
{ "_id" : ObjectId("536233445a87a26d8f721fa5"), "ltime" : ISODate("2012-11-08T07:58:55Z") }
{ "_id" : ObjectId("536233485a87a26d8f721fa6"), "ltime" : ISODate("2012-11-11T07:58:55Z") }
> db.xiaorui.test.find({'ltime':{gt:new Date("2012-11-05")}})
{ "_id" : ObjectId("536232e65a87a26d8f721fa2"), "ltime" : ISODate("2012-11-05T07:58:55Z") }
{ "_id" : ObjectId("536232e85a87a26d8f721fa3"), "ltime" : ISODate("2012-11-06T07:58:55Z") }
{ "_id" : ObjectId("5362333b5a87a26d8f721fa4"), "ltime" : ISODate("2012-11-07T07:58:55Z") }
{ "_id" : ObjectId("536233445a87a26d8f721fa5"), "ltime" : ISODate("2012-11-08T07:58:55Z") }
{ "_id" : ObjectId("536233485a87a26d8f721fa6"), "ltime" : ISODate("2012-11-11T07:58:55Z") }
> db.xiaorui.test.find({'ltime':{gt:new Date("2012-11-08")}})
{ "_id" : ObjectId("536233445a87a26d8f721fa5"), "ltime" : ISODate("2012-11-08T07:58:55Z") }
{ "_id" : ObjectId("536233485a87a26d8f721fa6"), "ltime" : ISODate("2012-11-11T07:58:55Z") }
> db.xiaorui.test.find({'ltime':{gt:new Date("2012-11-8")}})
{ "_id" : ObjectId("536233445a87a26d8f721fa5"), "ltime" : ISODate("2012-11-08T07:58:55Z") }
{ "_id" : ObjectId("536233485a87a26d8f721fa6"), "ltime" : ISODate("2012-11-11T07:58:55Z") }
> 
> 
> db.xiaorui.test.find({'ltime':{gt:new Date("2012-11-3")}})
{ "_id" : ObjectId("536232e05a87a26d8f721fa0"), "ltime" : ISODate("2012-11-03T07:58:55Z") }
{ "_id" : ObjectId("536232e35a87a26d8f721fa1"), "ltime" : ISODate("2012-11-04T07:58:55Z") }
{ "_id" : ObjectId("536232e65a87a26d8f721fa2"), "ltime" : ISODate("2012-11-05T07:58:55Z") }
{ "_id" : ObjectId("536232e85a87a26d8f721fa3"), "ltime" : ISODate("2012-11-06T07:58:55Z") }
{ "_id" : ObjectId("5362333b5a87a26d8f721fa4"), "ltime" : ISODate("2012-11-07T07:58:55Z") }
{ "_id" : ObjectId("536233445a87a26d8f721fa5"), "ltime" : ISODate("2012-11-08T07:58:55Z") }
{ "_id" : ObjectId("536233485a87a26d8f721fa6"), "ltime" : ISODate("2012-11-11T07:58:55Z") }
> db.xiaorui.test.insert({'ltime':ISODate("20121112115822")})
> db.xiaorui.test.find({'ltime':{gt:new Date("2012-11-3")}})
{ "_id" : ObjectId("536232e05a87a26d8f721fa0"), "ltime" : ISODate("2012-11-03T07:58:55Z") }
{ "_id" : ObjectId("536232e35a87a26d8f721fa1"), "ltime" : ISODate("2012-11-04T07:58:55Z") }
{ "_id" : ObjectId("536232e65a87a26d8f721fa2"), "ltime" : ISODate("2012-11-05T07:58:55Z") }
{ "_id" : ObjectId("536232e85a87a26d8f721fa3"), "ltime" : ISODate("2012-11-06T07:58:55Z") }
{ "_id" : ObjectId("5362333b5a87a26d8f721fa4"), "ltime" : ISODate("2012-11-07T07:58:55Z") }
{ "_id" : ObjectId("536233445a87a26d8f721fa5"), "ltime" : ISODate("2012-11-08T07:58:55Z") }
{ "_id" : ObjectId("536233485a87a26d8f721fa6"), "ltime" : ISODate("2012-11-11T07:58:55Z") }
{ "_id" : ObjectId("536233fd5a87a26d8f721fa7"), "ltime" : ISODate("2012-11-12T00:00:00Z") }
> 
> 
> 
> db.xiaorui.test.insert({'ltime':ISODate("20121114")})
> db.xiaorui.test.find({'ltime':{gt:new Date("2012-11-3")}})
{ "_id" : ObjectId("536232e05a87a26d8f721fa0"), "ltime" : ISODate("2012-11-03T07:58:55Z") }
{ "_id" : ObjectId("536232e35a87a26d8f721fa1"), "ltime" : ISODate("2012-11-04T07:58:55Z") }
{ "_id" : ObjectId("536232e65a87a26d8f721fa2"), "ltime" : ISODate("2012-11-05T07:58:55Z") }
{ "_id" : ObjectId("536232e85a87a26d8f721fa3"), "ltime" : ISODate("2012-11-06T07:58:55Z") }
{ "_id" : ObjectId("5362333b5a87a26d8f721fa4"), "ltime" : ISODate("2012-11-07T07:58:55Z") }
{ "_id" : ObjectId("536233445a87a26d8f721fa5"), "ltime" : ISODate("2012-11-08T07:58:55Z") }
{ "_id" : ObjectId("536233485a87a26d8f721fa6"), "ltime" : ISODate("2012-11-11T07:58:55Z") }
{ "_id" : ObjectId("536233fd5a87a26d8f721fa7"), "ltime" : ISODate("2012-11-12T00:00:00Z") }
{ "_id" : ObjectId("536234255a87a26d8f721fa8"), "ltime" : ISODate("2012-11-14T00:00:00Z") }
> db.xiaorui.test.insert({'ltime':ISODate("20121114 11:11:11")})
> db.xiaorui.test.find({'ltime':{gt:new Date("2012-11-3")}})
{ "_id" : ObjectId("536232e05a87a26d8f721fa0"), "ltime" : ISODate("2012-11-03T07:58:55Z") }
{ "_id" : ObjectId("536232e35a87a26d8f721fa1"), "ltime" : ISODate("2012-11-04T07:58:55Z") }
{ "_id" : ObjectId("536232e65a87a26d8f721fa2"), "ltime" : ISODate("2012-11-05T07:58:55Z") }
{ "_id" : ObjectId("536232e85a87a26d8f721fa3"), "ltime" : ISODate("2012-11-06T07:58:55Z") }
{ "_id" : ObjectId("5362333b5a87a26d8f721fa4"), "ltime" : ISODate("2012-11-07T07:58:55Z") }
{ "_id" : ObjectId("536233445a87a26d8f721fa5"), "ltime" : ISODate("2012-11-08T07:58:55Z") }
{ "_id" : ObjectId("536233485a87a26d8f721fa6"), "ltime" : ISODate("2012-11-11T07:58:55Z") }
{ "_id" : ObjectId("536233fd5a87a26d8f721fa7"), "ltime" : ISODate("2012-11-12T00:00:00Z") }
{ "_id" : ObjectId("536234255a87a26d8f721fa8"), "ltime" : ISODate("2012-11-14T00:00:00Z") }
{ "_id" : ObjectId("536234385a87a26d8f721fa9"), "ltime" : ISODate("2012-11-14T11:11:11Z") }
> db.xiaorui.test.insert({'ltime':new Date})
> db.xiaorui.test.find({'ltime':{gt:new Date("2012-11-3")}})
{ "_id" : ObjectId("536232e05a87a26d8f721fa0"), "ltime" : ISODate("2012-11-03T07:58:55Z") }
{ "_id" : ObjectId("536232e35a87a26d8f721fa1"), "ltime" : ISODate("2012-11-04T07:58:55Z") }
{ "_id" : ObjectId("536232e65a87a26d8f721fa2"), "ltime" : ISODate("2012-11-05T07:58:55Z") }
{ "_id" : ObjectId("536232e85a87a26d8f721fa3"), "ltime" : ISODate("2012-11-06T07:58:55Z") }
{ "_id" : ObjectId("5362333b5a87a26d8f721fa4"), "ltime" : ISODate("2012-11-07T07:58:55Z") }
{ "_id" : ObjectId("536233445a87a26d8f721fa5"), "ltime" : ISODate("2012-11-08T07:58:55Z") }
{ "_id" : ObjectId("536233485a87a26d8f721fa6"), "ltime" : ISODate("2012-11-11T07:58:55Z") }
{ "_id" : ObjectId("536233fd5a87a26d8f721fa7"), "ltime" : ISODate("2012-11-12T00:00:00Z") }
{ "_id" : ObjectId("536234255a87a26d8f721fa8"), "ltime" : ISODate("2012-11-14T00:00:00Z") }
{ "_id" : ObjectId("536234385a87a26d8f721fa9"), "ltime" : ISODate("2012-11-14T11:11:11Z") }
{ "_id" : ObjectId("536234585a87a26d8f721faa"), "ltime" : ISODate("2014-05-01T11:47:36.144Z") }
> 
> 
> 
> db.xiaorui.test.insert({'ltime':new})
Thu May  1 19:51:06.083 SyntaxError: Unexpected token }
> db.xiaorui.test.insert({'ltime':new Date()})
> db.xiaorui.test.find({'ltime':{gt:new Date("2012-11-3")}})
{ "_id" : ObjectId("536232e05a87a26d8f721fa0"), "ltime" : ISODate("2012-11-03T07:58:55Z") }
{ "_id" : ObjectId("536232e35a87a26d8f721fa1"), "ltime" : ISODate("2012-11-04T07:58:55Z") }
{ "_id" : ObjectId("536232e65a87a26d8f721fa2"), "ltime" : ISODate("2012-11-05T07:58:55Z") }
{ "_id" : ObjectId("536232e85a87a26d8f721fa3"), "ltime" : ISODate("2012-11-06T07:58:55Z") }
{ "_id" : ObjectId("5362333b5a87a26d8f721fa4"), "ltime" : ISODate("2012-11-07T07:58:55Z") }
{ "_id" : ObjectId("536233445a87a26d8f721fa5"), "ltime" : ISODate("2012-11-08T07:58:55Z") }
{ "_id" : ObjectId("536233485a87a26d8f721fa6"), "ltime" : ISODate("2012-11-11T07:58:55Z") }
{ "_id" : ObjectId("536233fd5a87a26d8f721fa7"), "ltime" : ISODate("2012-11-12T00:00:00Z") }
{ "_id" : ObjectId("536234255a87a26d8f721fa8"), "ltime" : ISODate("2012-11-14T00:00:00Z") }
{ "_id" : ObjectId("536234385a87a26d8f721fa9"), "ltime" : ISODate("2012-11-14T11:11:11Z") }
{ "_id" : ObjectId("536234585a87a26d8f721faa"), "ltime" : ISODate("2014-05-01T11:47:36.144Z") }
{ "_id" : ObjectId("536235365a87a26d8f721fab"), "ltime" : ISODate("2014-05-01T11:51:18.926Z") }
> 
> db.xiaorui.test.find({'ltime':{gt:new Date("2012-11-3")}})
{ "_id" : ObjectId("536232e05a87a26d8f721fa0"), "ltime" : ISODate("2012-11-03T07:58:55Z") }
{ "_id" : ObjectId("536232e35a87a26d8f721fa1"), "ltime" : ISODate("2012-11-04T07:58:55Z") }
{ "_id" : ObjectId("536232e65a87a26d8f721fa2"), "ltime" : ISODate("2012-11-05T07:58:55Z") }
{ "_id" : ObjectId("536232e85a87a26d8f721fa3"), "ltime" : ISODate("2012-11-06T07:58:55Z") }
{ "_id" : ObjectId("5362333b5a87a26d8f721fa4"), "ltime" : ISODate("2012-11-07T07:58:55Z") }
{ "_id" : ObjectId("536233445a87a26d8f721fa5"), "ltime" : ISODate("2012-11-08T07:58:55Z") }
{ "_id" : ObjectId("536233485a87a26d8f721fa6"), "ltime" : ISODate("2012-11-11T07:58:55Z") }
{ "_id" : ObjectId("536233fd5a87a26d8f721fa7"), "ltime" : ISODate("2012-11-12T00:00:00Z") }
{ "_id" : ObjectId("536234255a87a26d8f721fa8"), "ltime" : ISODate("2012-11-14T00:00:00Z") }
{ "_id" : ObjectId("536234385a87a26d8f721fa9"), "ltime" : ISODate("2012-11-14T11:11:11Z") }
{ "_id" : ObjectId("536234585a87a26d8f721faa"), "ltime" : ISODate("2014-05-01T11:47:36.144Z") }
{ "_id" : ObjectId("536235365a87a26d8f721fab"), "ltime" : ISODate("2014-05-01T11:51:18.926Z") }
> db.xiaorui.test.insert({'ltime':new Date().toLocaleString()})
> db.xiaorui.test.insert({'ltime':ISODate("20140501 19:56:11")})
> db.xiaorui.test.find({'ltime':{gt:new Date("2012-11-3")}})
{ "_id" : ObjectId("536232e05a87a26d8f721fa0"), "ltime" : ISODate("2012-11-03T07:58:55Z") }
{ "_id" : ObjectId("536232e35a87a26d8f721fa1"), "ltime" : ISODate("2012-11-04T07:58:55Z") }
{ "_id" : ObjectId("536232e65a87a26d8f721fa2"), "ltime" : ISODate("2012-11-05T07:58:55Z") }
{ "_id" : ObjectId("536232e85a87a26d8f721fa3"), "ltime" : ISODate("2012-11-06T07:58:55Z") }
{ "_id" : ObjectId("5362333b5a87a26d8f721fa4"), "ltime" : ISODate("2012-11-07T07:58:55Z") }
{ "_id" : ObjectId("536233445a87a26d8f721fa5"), "ltime" : ISODate("2012-11-08T07:58:55Z") }
{ "_id" : ObjectId("536233485a87a26d8f721fa6"), "ltime" : ISODate("2012-11-11T07:58:55Z") }
{ "_id" : ObjectId("536233fd5a87a26d8f721fa7"), "ltime" : ISODate("2012-11-12T00:00:00Z") }
{ "_id" : ObjectId("536234255a87a26d8f721fa8"), "ltime" : ISODate("2012-11-14T00:00:00Z") }
{ "_id" : ObjectId("536234385a87a26d8f721fa9"), "ltime" : ISODate("2012-11-14T11:11:11Z") }
{ "_id" : ObjectId("536234585a87a26d8f721faa"), "ltime" : ISODate("2014-05-01T11:47:36.144Z") }
{ "_id" : ObjectId("536235365a87a26d8f721fab"), "ltime" : ISODate("2014-05-01T11:51:18.926Z") }
{ "_id" : ObjectId("5362368c5a87a26d8f721faf"), "ltime" : ISODate("2014-05-01T19:56:11Z") }
> db.xiaorui.test.find({'ltime':{gt:new Date("2012-11-3")}})
{ "_id" : ObjectId("536232e05a87a26d8f721fa0"), "ltime" : ISODate("2012-11-03T07:58:55Z") }
{ "_id" : ObjectId("536232e35a87a26d8f721fa1"), "ltime" : ISODate("2012-11-04T07:58:55Z") }
{ "_id" : ObjectId("536232e65a87a26d8f721fa2"), "ltime" : ISODate("2012-11-05T07:58:55Z") }
{ "_id" : ObjectId("536232e85a87a26d8f721fa3"), "ltime" : ISODate("2012-11-06T07:58:55Z") }
{ "_id" : ObjectId("5362333b5a87a26d8f721fa4"), "ltime" : ISODate("2012-11-07T07:58:55Z") }
{ "_id" : ObjectId("536233445a87a26d8f721fa5"), "ltime" : ISODate("2012-11-08T07:58:55Z") }
{ "_id" : ObjectId("536233485a87a26d8f721fa6"), "ltime" : ISODate("2012-11-11T07:58:55Z") }
{ "_id" : ObjectId("536233fd5a87a26d8f721fa7"), "ltime" : ISODate("2012-11-12T00:00:00Z") }
{ "_id" : ObjectId("536234255a87a26d8f721fa8"), "ltime" : ISODate("2012-11-14T00:00:00Z") }
{ "_id" : ObjectId("536234385a87a26d8f721fa9"), "ltime" : ISODate("2012-11-14T11:11:11Z") }
{ "_id" : ObjectId("536234585a87a26d8f721faa"), "ltime" : ISODate("2014-05-01T11:47:36.144Z") }
{ "_id" : ObjectId("536235365a87a26d8f721fab"), "ltime" : ISODate("2014-05-01T11:51:18.926Z") }
{ "_id" : ObjectId("5362368c5a87a26d8f721faf"), "ltime" : ISODate("2014-05-01T19:56:11Z") }
> 
> db.xiaorui.test.find({'ltime':{$gt:new Date("2014-1-3")}})
{ "_id" : ObjectId("536234585a87a26d8f721faa"), "ltime" : ISODate("2014-05-01T11:47:36.144Z") }
{ "_id" : ObjectId("536235365a87a26d8f721fab"), "ltime" : ISODate("2014-05-01T11:51:18.926Z") }{ "_id" : ObjectId("5362368c5a87a26d8f721faf"), "ltime" : ISODate("2014-05-01T19:56:11Z") } 

python使用pymongo操作时间

str='2014-05-01 21:36'
date_time = datetime.strptime(str,'%Y-%m-%d %H:%M')
In [216]: for i in db.test.find({'ltime':{'$gt':date_time}}):
   .....:     print i
   .....:     
   .....:     
{u'ltime': datetime.datetime(2014, 5, 1, 21, 36, 31, 734000), u'_id': ObjectId('53624ddf2466d10c92f5e59e')}
{u'ltime': datetime.datetime(2014, 5, 1, 21, 37, 44, 334000), u'_id': ObjectId('53624e282466d10c92f5e59f')}
{u'ltime': datetime.datetime(2014, 5, 1, 21, 38, 55, 651000), u'_id': ObjectId('53624e6f2466d10c92f5e5a0')}
{u'ltime': datetime.datetime(2014, 5, 1, 21, 43, 13, 707000), u'_id': ObjectId('53624f712466d10c92f5e5a1')}
{u'ltime': datetime.datetime(2014, 5, 1, 21, 43, 30, 201000), u'_id': ObjectId('53624f822466d10c92f5e5a2')}
{u'ltime': datetime.datetime(2014, 5, 1, 21, 43, 45, 815000), u'_id': ObjectId('53624f912466d10c92f5e5a3')}
{u'ltime': datetime.datetime(2014, 5, 1, 21, 59, 28, 389000), u'_id': ObjectId('536253402466d10c92f5e5a4')}


大家觉得文章对你有些作用! 如果想赏钱,可以用微信扫描下面的二维码,感谢!
另外再次标注博客原地址  xiaorui.cc

发表评论

邮箱地址不会被公开。 必填项已用*标注