pymongo
import pymongofrom bson import ObjectIdimport jsonmongo_client=pymongo.MongoClient(host='127.0.0.1',port=27017) #连接数据库MONGO=mongo_client['xxx'] #找到所要查的表
"""#查询数据res=MONGO.xxx.find({}) #生成器for i in res: print(i)
res1=list(MONGO.xxx.find({}))print(res1)
res=MONGO.xxx.find_one({'id':20}) #去单个 用find_one# print(type(res),res.get('name'),res.get('_id'))res['_id']=str(res['_id'])print(res)res_json=json.dumps(res)print(res_json) #{"_id": "5c21f8ce902f3125d4f4c0ee", "id": 20, "name": 123}
res_obj=MONGO.xxx.find_one({'_id':ObjectId(res['_id'])})print(res_obj) #{'_id': ObjectId('5c21f8ce902f3125d4f4c0ee'), 'id': 20, 'name': 123}
# $or操作res=MONGO.xxx.find_one({'id':20})res1=list(MONGO.xxx.find({"$or":[{'id':20},{'name':123}]}))print(res1)"""
'''#增加数据res=MONGO.xxx.insert_one({'name':'qwe','age':13})print(res,res.inserted_id) #对象可以直接. res.inserted_id 5c21ff4311681e0858c20b91
res=MONGO.xxx.insert_many([{'name':'asd','age':12},{'name':'aaa','age':14}])print(res,res.inserted_ids) #存多个用res.inserted_ids'''
"""#修改数据
res=MONGO.xxx.update_one({'id':3},{'$set':{'id':6}})print(res,dir(res),res.raw_result)
res=MONGO.xxx.update_many({'id':2},{'$set':{'id':3}})print(res,dir(res),res.raw_result) #{'n': 38, 'nModified': 38, 'ok': 1.0, 'updatedExisting': True} #修改38个"""
"""#删除数据res=MONGO.xxx.delete_one({'id':6})res=MONGO.xxx.delete_many({'name':'qwe'})print(res,dir(res),res.raw_result)"""
"""#分页 limit 跳转 skip 排序 sortres=list(MONGO.xxx.find({"id":3}).limit(5))print(res,len(res))
res=list(MONGO.xxx.find({}).limit(5).skip(2))print(res,len(res))
#pymongo.DESCENDING res=list(MONGO.xxx.find({}).sort('id',-1).limit(5).skip(5))print(res,len(res))"""
"""#python 的update
res=MONGO.xxx.find_one({'id':3})print(res) #查看全部
#更改数据res.get('xxx')['shengao']=170res.get('xxx')['long']=20
#用对象来改数据MONGO.xxx.update_one({'_id':res.get('_id')},{'$set':res})res=MONGO.xxx.find_one({'id':3})print(res)
"""
pymongo的更多相关文章
- Python: Windows 7 64位 安装、使用 pymongo 3.2
官网tutorial: http://api.mongodb.com/python/current/tutorial.html 本教程将要告诉你如何使用pymongo模块来操作MongoDB数据库. ...
- 2.0 (2)测试pymongo
在数据库中创建数据库.表,插入数据. from pymongo import MongoClient host = "localhost" port = 27017 client ...
- Windows平台下为Python添加MongoDB支持PyMongo
到Python官网下载pymongo-2.6.3.win-amd64-py2.7.exe 安装pymongo-2.6.3.win-amd64-py2.7.exe 参照官方的用例进行测试 打开命令提示符 ...
- 【Python】pymongo使用
官方文档:http://api.mongodb.com/python/current/index.html MongoReplicaSetClient:http://api.mongodb.com/p ...
- 【mongo】pymongo通过_id删除数据
来源:http://www.educity.cn/wenda/361741.html pymongo 根据 objectId _id 来删除数据想要删除数据,根据_id ,是最靠谱的,具体方法因为 _ ...
- pymongo 3.3 使用笔记
#首先安装pymongo sudo pip install pymongo || sudo easy_install pymongo #demo均在交互解释器下进行 from pymongo impo ...
- PyMongo下载及安装
PyMongo最新版本下载地址: http://pypi.python.org/pypi/pymongo/#downloads PyMongo旧版本下载地址: http://pypi.python.o ...
- 【pymongo】连接认证 auth failed解决方法
故事背景: 我在虚拟机(ip:192.168.xx.xx)上建立了一个mongo的数据库,里面已经存好了内容.里面的一个database叫做 "adb", 里面有个collecti ...
- pymongo使用总结
0. 何为pymongo pymongo是操作MongoDB的python模块 1.安装pymongo # easy_install pymongo 2.连接mongodb >>> ...
- mongodb的python接口pymongo使用
1. 连接 from pymongo import MongoClient client = MongoClient("mongodb://mongodb0.example.net:2701 ...
随机推荐
- python多线程、多进程相关知识
Queue Queue用于建立和操作队列,常和threading类一起用来建立一个简单的线程队列. 首先,队列有很多种,根据进出顺序来分类,可以分成 Queue.Queue(maxsize) FIFO ...
- 在Hanlp词典手动添加未登录词的方式介绍
在使用Hanlp词典进行分词的时候,会出现分词不准的情况,原因是内置词典中并没有收录当前这个词,也就是我们所说的未登录词,只要把这个词加入到内置词典中就可以解决类似问题,如何操作呢,下面我们来看一下: ...
- RedHat6.5安装kafka单机
版本号: Redhat6.5 JDK1.8 zookeeper-3.4.6 kafka_2.11-0.8.2.1 1.软件环境 已经搭建好的zookeeper: RedHat6.5 ...
- Windows系统下MySQL数据库出现Access denied for user 'root'@'localhost' (using password:YES) 错误
Windows系统下MySQL数据库出现Access denied for user 'root'@'localhost' (using password:YES) 错误,(root密码错误) 处理方 ...
- Spring Cloud Feign Ribbon 配置
由于 Spring Cloud Feign 的客户端负载均衡是通过 Spring Cloud Ribbon 实现的,所以我们可以直接通过配置 Ribbon 的客户端的方式来自定义各个服务客户端调用的参 ...
- Linux之用户组、文件权限详解
用户和用户组 文件所有者(u) 一般为文件的创建者,谁创建了该文件,就天然的成为该文件的所有者 用ls ‐ahl命令可以看到文件的所有者 也可以使用chown 用户名 文件名来修改文件的所有者 用户组 ...
- 银行卡所属公司判断 参考自https://blog.csdn.net/well2049/article/details/79429130
在网上找到了一个银行卡的验证,通过阿里的支付宝接口进行校验,能够准确识别是否存在,归属行,卡号类型是储蓄卡(DC)还是信用卡(CC). 接口api:需要传入的2个参数,卡号cardNo和cardBin ...
- debian下erlang新版本安装
debian下的erlang版本太老 安装kerl,并且在profile内添加到PATH curl -o ~/dev/erlang/kerl https://raw.githubusercontent ...
- HP Gen8,9 型号系列服务器更换主板
更换主板前,记下如下信息,根据具体情况用于更换后的设置用.1.S/N (其实主机箱上会写有,更换后重置)2.ProductID (其实主机箱上会写有,更换后重置)3.iLO IP地址或者MAC地址(根 ...
- sqlserver 查询表中的主键、外键列及外键表,外表中的主键列
1.获取主键信息 EXEC sp_pkeys @table_name='{0}' 2.获取外键 方法二 SELECT Field=(SELECT name FROM syscolumns WHERE ...