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的更多相关文章

  1. Python: Windows 7 64位 安装、使用 pymongo 3.2

    官网tutorial:  http://api.mongodb.com/python/current/tutorial.html 本教程将要告诉你如何使用pymongo模块来操作MongoDB数据库. ...

  2. 2.0 (2)测试pymongo

    在数据库中创建数据库.表,插入数据. from pymongo import MongoClient host = "localhost" port = 27017 client ...

  3. Windows平台下为Python添加MongoDB支持PyMongo

    到Python官网下载pymongo-2.6.3.win-amd64-py2.7.exe 安装pymongo-2.6.3.win-amd64-py2.7.exe 参照官方的用例进行测试 打开命令提示符 ...

  4. 【Python】pymongo使用

    官方文档:http://api.mongodb.com/python/current/index.html MongoReplicaSetClient:http://api.mongodb.com/p ...

  5. 【mongo】pymongo通过_id删除数据

    来源:http://www.educity.cn/wenda/361741.html pymongo 根据 objectId _id 来删除数据想要删除数据,根据_id ,是最靠谱的,具体方法因为 _ ...

  6. pymongo 3.3 使用笔记

    #首先安装pymongo sudo pip install pymongo || sudo easy_install pymongo #demo均在交互解释器下进行 from pymongo impo ...

  7. PyMongo下载及安装

    PyMongo最新版本下载地址: http://pypi.python.org/pypi/pymongo/#downloads PyMongo旧版本下载地址: http://pypi.python.o ...

  8. 【pymongo】连接认证 auth failed解决方法

    故事背景: 我在虚拟机(ip:192.168.xx.xx)上建立了一个mongo的数据库,里面已经存好了内容.里面的一个database叫做 "adb", 里面有个collecti ...

  9. pymongo使用总结

    0. 何为pymongo pymongo是操作MongoDB的python模块 1.安装pymongo # easy_install pymongo 2.连接mongodb >>> ...

  10. mongodb的python接口pymongo使用

    1. 连接 from pymongo import MongoClient client = MongoClient("mongodb://mongodb0.example.net:2701 ...

随机推荐

  1. unicodedata.normalize()/使用strip()、rstrip()和lstrip()/encode和decode 笔记(具体可看 《Python Cookbook》3rd Edition 2.9~2.11)

    unicodedata.normalize()清理字符串 # normalize()的第一个参数指定字符串标准化的方式,分别有NFD/NFC >>> s1 = 'Spicy Jala ...

  2. 如何重写hashCode()和equals()方法

    hashCode()和equals()方法可以说是Java完全面向对象的一大特色.它为我们的编程提供便利的同时也带来了很多危险.这篇文章我们就讨论一下如何正解理解和使用这2个方法. 如何重写equal ...

  3. 【java】一维数组

    数组概念: 同一种类型数据的集合,实际数组也是一个容器. 定义方式: //定义方法1:元素类型 [] 数组名 =new 元素类型 [数组元素个数或数组长度] 如 int [] arry =new in ...

  4. GTX的生成(包括COMMON)

    GTX的生成(包括COMMON) 1.每一个GTX Quad需要一个GTX common,同时GTX common只包含有QPLL,不包含CPLL. 2.kintex-7设备只支持GTX 3.参考时钟 ...

  5. HP Gen8,9 型号系列服务器更换主板

    更换主板前,记下如下信息,根据具体情况用于更换后的设置用.1.S/N (其实主机箱上会写有,更换后重置)2.ProductID (其实主机箱上会写有,更换后重置)3.iLO IP地址或者MAC地址(根 ...

  6. VS项目平台的x86,x64,Any CPU以及Debug和Release

    引用链接:https://blog.csdn.net/zuguangboy/article/details/51509670

  7. [蓝桥杯]ALGO-15.算法训练_旅行家的预算

    问题描述 一个旅行家想驾驶汽车以最少的费用从一个城市到另一个城市(假设出发时油箱是空的).给定两个城市之间的距离D1.汽车油箱的容量C(以升为单位).每升汽油能行驶的距离D2.出发点每升汽油价格P和沿 ...

  8. JAVA中Integer类型变量比较问题

    今天在做实验的时候,发现了一个比较奇怪的问题:两个Integer型变量用==进行比较时,有时候能成功有时候不能成功.举个例子: 代码1: Integer l1 = 122; Integer l2 = ...

  9. P2064进制转换

    题目:https://www.luogu.org/problemnew/show/P2084 既然这道题放在字符串类型里,那么这里的N肯定得用字符数组来储存(这样也方便输出). 那么我们不妨定义一个字 ...

  10. Underscore.js(1.9.1) 封装库

    // Underscore.js 1.9.1// http://underscorejs.org// (c) 2009-2018 Jeremy Ashkenas, DocumentCloud and ...