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 ...
随机推荐
- 2、Sql-oracle创建新用户
1.首先我们可以用scott用户以sysdba的身份登录oracle. conn scott/tiger as sysdba 2.然后我就可以来创建用户了. create user zs identi ...
- eclipse卡死在search for main types 20 files to index
run as application时,提示search for main types 20 files to index (*/*/*.jar)某个maven依赖jar出了问题,找不到main方法 ...
- Spring 集成Junit单元测试
1.在pom增加junit和spring-test <dependency> <groupId>junit</groupId> <artifactId> ...
- Spring Cloud(Dalston.SR5)--Eureka 服务提供者
要使微服务应用向注册中心发布自己,首先需要在 pom.xml 配置文件中增加对 spring-boot-starter-eureka 的依赖,然后在主类中增加 @EnableDiscoveryClie ...
- mysql 1267 error
CREATE TABLE a (id VARCHAR(32)) DEFAULT CHARSET = utf8 COLLATE utf8_general_ci:INSERT INTO a(id) VAL ...
- Ubuntu PPA软件源
PPA,其英文全称为 Personal Package Archives,即个人软件包档案.是 Ubuntu Launchpad 网站提供的一项源服务,允许个人用户上传软件源代码,通过 Launchp ...
- 无需AutoCAD,用C#生成DWG文件
是一个类库:Teigha.NET for .DWG 利用它就可以在无需安装AutoCAD软件的情况下,生成.读取DWG文件,适合那些导入导出的场合. Teigha曾用名OpenDWG .DWGdire ...
- ALGO-22_蓝桥杯_算法训练_数的划分(DP)
问题描述 将整数n分成k份,且每份不能为空,任意两份不能相同(不考虑顺序). 例如:n=,k=,下面三种分法被认为是相同的. ,,; ,,; ,,; 问有多少种不同的分法. 输入格式 n,k 输出格式 ...
- 【Zabbix3.0】之入门到精通
https://www.cnblogs.com/clsn/p/7885990.html 饿了么技术债 http://server.51cto.com/sOS-555999.htm
- 【Mysql】MySQL高效获取记录总数SQL_CALC_FOUND_ROWS
用法说明 分页程序一般由两条SQL组成: SELECT COUNT(*) FROM ... WHERE .... SELECT ... FROM ... WHERE LIMIT ... 如果使用SQL ...