MongoDB是一个存储文档型的数据库(非关系型数据库)
利用pymongo连接MongoDB
复制 | import pymongo |
| client = pymongo.MongoClient(host='localhost', port=27017) |
| |
| |
指定数据库
复制 | |
| db = client.test 或 db = client['test'] |
指定集合
复制 | |
| collection = db.students 或 collection = db['students'] |
插入数据
插入单条数据
复制
复制 | import pymongo |
| |
| |
| client = pymongo.MongoClient(host='localhost', port=27017) |
| |
| |
| db = client.test |
| |
| |
| collection = db.students |
| |
| |
| student = { |
| 'id': '20180001', |
| 'name': 'Jordan', |
| 'age': 20, |
| 'gender': 'male' |
| } |
| |
| |
| result = collection.insert_one(student) |
| print(result) |
| |
| |
| |
| |
插入多条数据
复制
复制 | import pymongo |
| |
| client = pymongo.MongoClient(host='localhost', port=27017) |
| db = client.test |
| collection = db.students |
| |
| student1 = { |
| 'id': '20180002', |
| 'name': 'Lee Hua', |
| 'age': 20, |
| 'gender': 'male' |
| } |
| student2 = { |
| 'id': '20180003', |
| 'name': 'Mike', |
| 'age': 21, |
| 'gender': 'male' |
| } |
| |
| result = collection.insert_many([student1, student2]) |
| print(result) |
| print(result.inserted_ids) |
| |
| |
| |
| |
| <pymongo.results.InsertManyResult object at 0x110826d88> |
| [ObjectId('5d28b293e834575faf929428'), ObjectId('5d28b293e834575faf929429')] |
查询
查询一条数据
复制
复制 | import pymongo |
| |
| client = pymongo.MongoClient(host='localhost', port=27017) |
| db = client.test |
| collection = db.students |
| |
| result = collection.find_one({'name': 'Lee Hua'}) |
| print(result) |
| |
| |
| |
| {'_id': ObjectId('5d28b293e834575faf929428'), 'id': '20180002', 'name': 'Lee Hua', 'age': 20, 'gender': 'male'} |
查询多条数据
复制
复制 | import pymongo |
| |
| client = pymongo.MongoClient(host='localhost', port=27017) |
| db = client.test |
| collection = db.students |
| |
| result = collection.find() |
| print(result) |
| for r in result: |
| print(r) |
| |
| |
| |
| |
| <pymongo.cursor.Cursor object at 0x10e0f7320> |
| {'_id': ObjectId('5d28ae0360105a198d9d501a'), 'id': '20180001', 'name': 'Jordan', 'age': 20, 'gender': 'male'} |
| {'_id': ObjectId('5d28ae2d8b3d004feb604874'), 'id': '20180001', 'name': 'Jordan', 'age': 20, 'gender': 'male'} |
| {'_id': ObjectId('5d28b293e834575faf929428'), 'id': '20180002', 'name': 'Lee Hua', 'age': 20, 'gender': 'male'} |
| {'_id': ObjectId('5d28b293e834575faf929429'), 'id': '20180003', 'name': 'Mike', 'age': 21, 'gender': 'male'} |
可以在这两个方法里面添加条件,如:
复制 | find( |
| { |
| 'name': {'$regex': '^M.*'} |
| } |
| ) |
| 这里查找的是以'M'开头的名字的那些数据, |
| $regex指定的是正则表达式, |
| ^M.*是一条正则表达式 |
| 更多功能符号(如$regex)、数值比较符号的查看MongoDB官方文档:https://docs.mongodb.com/?searchProperty=manual |
计数
复制 | import pymongo |
| |
| client = pymongo.MongoClient(host='localhost', port=27017) |
| db = client.test |
| collection = db.students |
| |
| count = collection.count_documents( |
| { |
| 'id': {'$regex': '^(2018)'} |
| } |
| ) |
| |
| print(count) |
排序
复制 | import pymongo |
| |
| client = pymongo.MongoClient(host='localhost', port=27017) |
| db = client.test |
| collection = db.students |
| |
| result = collection.find().sort('id', pymongo.ASCENDING) |
| for r in result: |
| print(r) |
| |
| |
| |
| {'_id': ObjectId('5d28ae0360105a198d9d501a'), 'id': '20180001', 'name': 'Jordan', 'age': 20, 'gender': 'male'} |
| {'_id': ObjectId('5d28ae2d8b3d004feb604874'), 'id': '20180001', 'name': 'Jordan', 'age': 20, 'gender': 'male'} |
| {'_id': ObjectId('5d28b293e834575faf929428'), 'id': '20180002', 'name': 'Lee Hua', 'age': 20, 'gender': 'male'} |
| {'_id': ObjectId('5d28b293e834575faf929429'), 'id': '20180003', 'name': 'Mike', 'age': 21, 'gender': 'male'} |
| |
| |
| |
| |
| |
偏移
skip() 方法
复制 | import pymongo |
| |
| client = pymongo.MongoClient(host='localhost', port=27017) |
| db = client.test |
| collection = db.students |
| |
| results = collection.find().sort('id', pymongo.DESCENDING).skip(1) |
| print( |
| [ |
| result['id'] for result in results |
| ] |
| ) |
| |
| |
| |
| ['20180002', '20180001', '20180001'] |
复制
limit()方法
复制 | import pymongo |
| |
| client = pymongo.MongoClient(host='localhost', port=27017) |
| db = client.test |
| collection = db.students |
| |
| results = collection.find().sort('id', pymongo.DESCENDING).skip(1).limit(2) |
| print( |
| [ |
| result['id'] for result in results |
| ] |
| ) |
| |
| |
| ['20180002', '20180001'] |
复制
注意:数据量很大时,不使用大的偏移量来查询数据。
更新
update_one()方法
复制 | import pymongo |
| |
| client = pymongo.MongoClient(host='localhost', port=27017) |
| db = client['test'] |
| collection = db['students'] |
| |
| |
| query_condition = { |
| 'age': {'$gte': 20} |
| } |
| |
| |
| update_condition = { |
| '$inc': {'age': 1} |
| } |
| |
| result = collection.update_one(query_condition, update_condition) |
| print(result) |
| print(result.matched_count, result.modified_count) |
| |
| |
| |
| <pymongo.results.UpdateResult object at 0x110a11c88> |
| 1 1 |
| |
| |
| |
| |
| |
| |
| |
| |
update_many()方法
复制 | import pymongo |
| |
| client = pymongo.MongoClient(host='localhost', port=27017) |
| db = client['test'] |
| collection = db['students'] |
| |
| query_condition = { |
| 'age': {'$gte': 20} |
| } |
| update_condition = { |
| '$inc': {'age': 1} |
| } |
| |
| result = collection.update_many(query_condition, update_condition) |
| print(result) |
| print(result.matched_count, result.modified_count) |
| |
| |
| |
| <pymongo.results.UpdateResult object at 0x111c84448> |
| 4 4 |
删除
delete_one() 方法
复制 | import pymongo |
| |
| client = pymongo.MongoClient(host='localhost', port=27017) |
| db = client['test'] |
| collection = db['students'] |
| |
| result = collection.delete_one({'age': 21}) |
| print(result.deleted_count) |
| |
| |
| |
| |
delete_many() 方法
复制 | import pymongo |
| |
| client = pymongo.MongoClient(host='localhost', port=27017) |
| db = client['test'] |
| collection = db['students'] |
| |
| result = collection.delete_many({'age': 21}) |
| print(result.deleted_count) |
| |
| |
| |
PyMongo的详细用法
官方文档:http://api.mongodb.com/python/current/api/pymongo/collection.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)