pip install pymongo
import pymongo
# 创建连接
client = pymongo.MongoClient(host="10.0.2.38", port=27017)
# 连接probeb库
db = client['probeb']
# 打印库中所有集合名称
print(db.collection_names())
# 连接到test1这个集合
collection = db.test1
# 这条命令是查找rssi大于srssi小于erssi,stime大于stime,小于etime的数据以stime倒叙排列
sumdata = collection.find({"RSSI": {"$gt": int(srssi), "$lt": int(erssi)}, "stime": {"$gt": stime, "$lt": etime}}).sort([('stime', -1)])
#这条命令是查找rssi大于srssi小于erssi,stime大于stime小于etime 且mac等于search或者dmac等于search(search是个变量, "$options":"i"是为了不区分search内容的大小写)的数据,以stime倒叙排列
sumdata = collection.find({"RSSI": {"$gt": int(srssi), "$lt": int(erssi)}, "stime": {"$gt": stime, "$lt": etime}, "$or": [{"mac": {"$regex": search, "$options":"i"}}, {"dmac": {"$regex": search,"$options":"i"}}]}).sort([('stime', -1)])
# 现在查询的结果赋值给sumdata,如果想查出具体数据,可以使用for循环
for data in sumdata:
print(data)
# 注意:在使用python操作MongoDB进行排序的时候,不能使用db.test1.find().sort({"name" : 1, "age" : 1})
# 否则会遇到如下异常:
# TypeError: if no direction is specified, key_or_list must be an instance of list
# 解决方法:
# db.tes1t.find().sort([("name", 1), ("age" , 1)])
# 原因:在python中只能使用列表进行排序,不能使用字典
# 插入数据
account = {"AccountID":1,"UserName":"libing",'date':datetime.datetime.now()}
accounts = [{"AccountID":2,"UserName":"liuw",'date':datetime.datetime.now()},
{"AccountID":3,"UserName":"urling",'date':datetime.datetime.now()}]#每条记录插入时间都
collections.insert(account)
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有