pymongo
Posted baijinshuo
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了pymongo相关的知识,希望对你有一定的参考价值。
python操作MongoDB,需要安装模块 pymongo
pip install pymongo
连接
新建一个文件 test_mongo.py
import pymongo # 连接数据库 mclient = pymongo.MongoClient(host="127.0.0.1", port=27017) mongo_db = mclient["testdb"] # 切换数据库,不存在则创建 res = mongo_db.goods.find() print(res)
执行输出:
<pymongo.cursor.Cursor object at 0x000001F68B7D09E8>
插入数据
import pymongo # 连接数据库 mclient = pymongo.MongoClient(host="127.0.0.1", port=27017) mongo_db = mclient["testdb"] # 切换数据库,不存在则创建 # 插入多条数据 res = mongo_db.goods.insert_many([{"name" : "酸菜","price" : "1"},{"name" : "白菜","price" : "2"},{"name" : "小青菜","price" : "0.5"},]) print(res)
查询数据
查询所有
可以将结果转换为列表
import pymongo # 连接数据库 mclient = pymongo.MongoClient(host="127.0.0.1", port=27017) mongo_db = mclient["testdb"] # 切换数据库,不存在则创建 # 插入多条数据 res = mongo_db.goods.find() print(list(res))
执行输出:
[{‘_id‘: ObjectId(‘5b991895e125324a58178dd3‘), ‘name‘: ‘酸菜‘, ‘price‘: ‘1‘}, {‘_id‘: ObjectId(‘5b991895e125324a58178dd4‘), ‘name‘: ‘白菜‘, ‘price‘: ‘2‘}, {‘_id‘: ObjectId(‘5b991895e125324a58178dd5‘), ‘name‘: ‘小青菜‘, ‘price‘: ‘0.5‘}]
也可以对结果做for循环
import pymongo # 连接数据库 mclient = pymongo.MongoClient(host="127.0.0.1", port=27017) mongo_db = mclient["testdb"] # 切换数据库,不存在则创建 # 插入多条数据 res = mongo_db.goods.find() for i in res: print(i)
执行输出:
{‘_id‘: ObjectId(‘5b991895e125324a58178dd3‘), ‘name‘: ‘酸菜‘, ‘price‘: ‘1‘} {‘_id‘: ObjectId(‘5b991895e125324a58178dd4‘), ‘name‘: ‘白菜‘, ‘price‘: ‘2‘} {‘_id‘: ObjectId(‘5b991895e125324a58178dd5‘), ‘name‘: ‘小青菜‘, ‘price‘: ‘0.5‘}
查询单条
import pymongo # 连接数据库 mclient = pymongo.MongoClient(host="127.0.0.1", port=27017) mongo_db = mclient["testdb"] # 切换数据库,不存在则创建 # 插入多条数据 res = mongo_db.goods.find_one() print(res)
执行输出:
{‘_id‘: ObjectId(‘5b991895e125324a58178dd3‘), ‘name‘: ‘酸菜‘, ‘price‘: ‘1‘}
查询指定id呢?
由于_id是ObjectId对象,需要导入模块
import pymongo from bson import ObjectId # 连接数据库 mclient = pymongo.MongoClient(host="127.0.0.1", port=27017) mongo_db = mclient["testdb"] # 切换数据库,不存在则创建 # 插入多条数据 res = mongo_db.goods.find({"_id":ObjectId("5b991895e125324a58178dd5")}) for i in res: print(i)
执行输出:
{‘_id‘: ObjectId(‘5b991895e125324a58178dd5‘), ‘name‘: ‘小青菜‘, ‘price‘: ‘0.5‘}
更新
import pymongo from bson import ObjectId # 连接数据库 mclient = pymongo.MongoClient(host="127.0.0.1", port=27017) mongo_db = mclient["testdb"] # 切换数据库,不存在则创建 res = mongo_db.goods.update_one({"name":"小青菜"},{"$set":{"price":2}}) print(res)
执行输出:
<pymongo.results.UpdateResult object at 0x000001AF61E332C8> None
删除
import pymongo from bson import ObjectId # 连接数据库 mclient = pymongo.MongoClient(host="127.0.0.1", port=27017) mongo_db = mclient["testdb"] # 切换数据库,不存在则创建 res = mongo_db.goods.delete_one({"name":"小青菜"}) print(res,res.raw_result)
执行输出:
<pymongo.results.DeleteResult object at 0x000001825B2134C8> {‘n‘: 0, ‘ok‘: 1.0}
排序
import pymongo from bson import ObjectId # 连接数据库 mclient = pymongo.MongoClient(host="127.0.0.1", port=27017) mongo_db = mclient["testdb"] # 切换数据库,不存在则创建 res = mongo_db.goods.find({}).skip(1).limit(2) print(list(res))
执行输出:
[{‘_id‘: ObjectId(‘5b991895e125324a58178dd4‘), ‘name‘: ‘白菜‘, ‘price‘: ‘2‘}]
进阶
import pymongo from bson import ObjectId # 连接数据库 mclient = pymongo.MongoClient(host="127.0.0.1", port=27017) mongo_db = mclient["testdb"] # 切换数据库,不存在则创建 res = mongo_db.goods.find({}).sort("age",pymongo.DESCENDING) print(list(res))
执行输出:
[{‘_id‘: ObjectId(‘5b991895e125324a58178dd3‘), ‘name‘: ‘酸菜‘, ‘price‘: ‘1‘}, {‘_id‘: ObjectId(‘5b991895e125324a58178dd4‘), ‘name‘: ‘白菜‘, ‘price‘: ‘2‘}]
高级操作
import pymongo from bson import ObjectId # 连接数据库 mclient = pymongo.MongoClient(host="127.0.0.1", port=27017) mongo_db = mclient["testdb"] # 切换数据库,不存在则创建 res = mongo_db.goods.find({}).sort("age",pymongo.DESCENDING).skip(1).limit(2) print(list(res))
执行输出:
[{‘_id‘: ObjectId(‘5b991895e125324a58178dd4‘), ‘name‘: ‘白菜‘, ‘price‘: ‘2‘}]
以上是关于pymongo的主要内容,如果未能解决你的问题,请参考以下文章