1、在mongodb中按时间字段查询的方法:
先将时间通过datetime()方法进行转换,再进行查询
>t1 = datetime(2017,month,day,hour1,59,59,000)
>t2 = datetime(2017,month,day,hour2,59,59,000)
>a = collection.find({"date": {"$gt":t1,"$lte":t2},"cdata.deviceName" : name})
2、有关datetime.timedelta:
datetime类型数据相减后为timedelta类型
>t1 = datetime(2017,3,24,23,59,59,000)
>t2 = datetime(2017,3,25,6,45,12,568)
>t3 = t2 - t1
>type(t3)
><type 'datetime.timedelta'>
可以用timedelta类型做关于时间的加减乘除运算:
>sum_time = 0:12:04.923000
>v = sum_time.total.second() #将时间变为秒数
>ave = sum/7200
>print ave
> 0.1008375
3、关于mongodb:
连接数据库
> from pymongo import MongoClient
>client=MongoClient('mongodb://admin:admin@10.10.30.66/mongo',30001)
db = client.dbname
db.authenticate("user","passwd")
collection = db.collname
查询
> results = collection.find({json_record},{field})
> results.count() #查询到的结果数
> for data in results:
s = data.items() #查询到的结果的字段
关于查询条件
>$gte : 大于或等于
$lte:小于或等于
$ne:不等于