pymongo
直接练习:操作和之前的基本类似
import pymongo from bson import ObjectId import json # 1.连接表 mongo_client = pymongo.MongoClient(host="127.0.0.1", port=27017) # 实例化一个客户端 mongo = mongo_client["mydb1"] # 连接那个库 # print(mongo) # Database(MongoClient(host=['127.0.0.1:27017'], document_class=dict, tz_aware=False, connect=True), 'mydb1') # 2.查看表和数据 # res = mongo.table1.find() # 拿到迭代器 <pymongo.cursor.Cursor object at 0x053046D0> # res = list(mongo.table1.find()) # 拿到表中所有数据 # # for i in res: # print(i) # 打印每个数据 # res = mongo.table1.find_one({"age": 1}) # # print(res) # {'_id': ObjectId('5c2227db1458801c38365ee7'), 'name': 1, 'age': 1} # # 发现res["_id"]拿到的是 bson的格式 # print(res["_id"], type(res["_id"])) # 5c2227db1458801c38365ee7 <class 'bson.objectid.ObjectId'> # # res["_id"] = str(res["_id"]) # 想使用 _id 可以用str强转一下 # # print(res["_id"], type(res["_id"])) # 5c2227db1458801c38365ee7 <class 'str'> # # # # 3.ObjectId json操作 # # res_obj = mongo.table1.find_one({"_id": res["_id"]}) # 通过唯一标识获得数据,此时被转换为str,获值为空 # res_obj = mongo.table1.find_one({"_id": ObjectId(res["_id"])}) # 通过唯一标识获得数据,转换位bson就可以了 # # print(type(res)) # <class 'dict'> # # print(res.get("name")) # 1 # # print(type(res_obj.get("_id"))) # <class 'bson.objectid.ObjectId'> # 获取的是bson格式对象 # # # res_json = json.dumps(res) # # print(res_json) # {"_id": "5c2227db1458801c38365ee7", "name": 1, "age": 1} # # print(type(res_json)) # <class 'str'> # 4.增加数据 # res = mongo.table1.insert_one({"name": "pymongo", "age": 666}) # print(res) # <pymongo.results.InsertOneResult object at 0x058F5698> # print(res.inserted_id) # 5c223ad3381e9239c8f72516 # res = mongo.table1.insert_many([{"name": "pymongo1", "age": 666}, {"name": "pymongo2", "age": 666}]) # print(res, res.inserted_ids) # 注意此时取 _id 时候需要 insert_ids # for i in res.inserted_ids: # print(i) # # 5.修改数据 # 所有age是666的全部修改 name是all_change age是999 # res = mongo.table1.update_many({"age": 666}, {"$set": {"name": "all_change", "age": 999}}) # print(res) # <pymongo.results.UpdateResult object at 0x04FC66E8> # 6.删除数据 # res = mongo.table1.delete_one({"age": 999}) # {'n': 1, 'ok': 1.0} # 删除一条 # res = mongo.table1.delete_one({"_id": ObjectId('5c223b77381e924cd4ce5672')}) # res = mongo.table1.delete_many({"age": 999}) # {'n': 2, 'ok': 1.0} 删除2条 # print(res) # <pymongo.results.DeleteResult object at 0x053B5788> # # print(dir(res)) # print(res.raw_result) # 删除的结果和状态 # 7.skip sort limit # res = list(mongo.table1.find({}).limit(5)) # print(len(res), res) # 查找5条数据 # # res = list(mongo.table1.find({}).limit(5).skip(5)) # print(len(res), res) # 跳过5条数据再查5条数据 # # # res = list(mongo.table1.find({}).sort("age", pymongo.DESCENDING)) # pymongo.DESCENDING:反序其实就是-1 # print(len(res), res) # 所有数据反序下 # res = list(mongo.table1.find({}).sort("age", pymongo.DESCENDING).skip(5).limit(2)) # print(res) # 反序后 跳5条数据然后 再展示2条数据 #非常适合分页0.0 # 8.python 的 update # res = mongo.table1.find_one({"name": "ming"}) # 获取table1中的"name": "ming" 的数据 # print(res) # # {'_id': ObjectId('5c220d658db659c8088a8d95'), 'name': 'ming', 'age': 18.0, 'info': {'tizhong': 125.0, 'gao': 175.0}, 'hobby': [3.0, 6.0, 4.0, 5.0]} # res.get("info")["tizhong"] = 126 # res.get("info")["gao"] = 169 # # # res 此时修改完毕 # mongo.table1.update_one({"_id": res.get("_id")}, {"$set": res}) # 直接简单粗暴的覆盖就可以了 # res = mongo.table1.find_one({"name": "ming"}) # print(res) # # {'_id': ObjectId('5c220d658db659c8088a8d95'), 'name': 'ming', 'age': 18.0, 'info': {'tizhong': 126, 'gao': 169}, 'hobby': [3.0, 6.0, 4.0, 5.0]}