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 ...
随机推荐
- activemq jms使用
activemq 是 基于 jms 协议的 消息队列 消息 流程: jsm 的 消息流程鼻尖简单 生产者发送消息到目的地,消费者 监听这个目的地,然后收到消息. 相比 amqp 的 消息流程简单很多 ...
- Oracle 增加、修改、删除字段
分别对T_USER表 进行增加name字段, 修改name字段,删除name字段 /*增加列表*/ ALTER TABLE T_USERS ADD name varchar2(512) ; /*删除列 ...
- RedHat7.3创建本地yum源
[root@master ~]# mkdir -p /var/www/html 使用安装系统的ISO镜像文件rhel-server-7.3-x86_64-dvd.iso 把rhel-server-7. ...
- CoreException: Could not get the value for parameter compilerId for plugin execution default-compile Maven项目pom文件报错,插件引用不到
CoreException: Could not get the value for parameter compilerId for plugin execution default-compile ...
- centos7 tomcat自启动
第一步: vim /lib/systemd/system/tomcat.service [Unit] Description=tomcat After=network.target [Service] ...
- Video Processing subsystem例程分析
Video Processing subsystem例程分析 1.memory_ss模块 slave端口: S00: 连接设备: microblaze_ss----M_AXI_DC 时钟来源: S01 ...
- Console下Pause效果的函数
function PauseConsole(Prompt: PAnsiChar): boolean;var hStdIn, hStdOut: THandle; dwRd, dwWr, i: Car ...
- WPF Demo14 依赖属性
using System.Windows; namespace DependencyPropertyDemo1 { public class Student:DependencyObject { pu ...
- Could not write to output file 'c:\Windows\Microsoft.NET ASP.NET Files\xx' -- 'Access is denied
网上有IIS7的解决方法,是给"C:\Windows\Temp"文件夹加上添加用户IIS_IUSRS的完全控制权限. 但我这个老机器是IIS6的,没有IIS_IUSERS用户,只能 ...
- Node.js 0.12: 正确发送HTTP POST请求
Node.js 0.12: 正确发送HTTP POST请求 本文针对版本:Node.js 0.12.4 之前写过一篇Node.js发送和接收HTTP的GET请求的文章,今天再写一篇,讲发送POST的请 ...