• Python使用PyMongo4.x操作MongoDB总结


    Python操作MongoDB

    概述

    PyMongo是一个Python编程语言中用于连接和操作MongoDB数据库的库。它提供了丰富的功能和API,使开发者能够在Python中轻松地进行MongoDB的数据交互和管理。

    MongoDB 驱动程序:https://api.mongodb.com/

    PyMongo文档:https://pymongo.readthedocs.io/en/stable/api/index.html

    MongoDB文档:https://www.mongodb.com/docs/manual/

    安装Python库

    pip install pymongo==4.3.3
    
    • 1

    注意:不同版本的PyMongo在语法和API方面可能会有差异一定差异

    连接、认证

    # 导入模块
    from pymongo import *
    
    # 创建客户端对象,使用账号登录
    # 方式一:
    '''
    host:连接的主机名或IP地址,默认为localhost
    port:连接使用的端口号,默认为27017
    username:用于身份验证的用户名,默认为None
    password:用于身份验证的密码,默认为None
    authSource:用于身份验证的数据库,默认为None,表示使用admin数据库
    '''
    client = MongoClient(host="ip", port=27017, username="test", password="123456", authSource="demo")
    
    # 方式二:
    # url = "mongodb://test:123456@ip/demo"
    # client = MongoClient(url)
    
    
    # 断开与MongoDB的连接
    client.close()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    创建数据库、集合

    # 判断数据库是否已存在
    dblist = client.list_database_names()
    if "demo" in dblist:
      print("demo 数据库已存在!")
    
    
    # 创建、获取数据库对象
    db = client.demo
    db = client['demo']
    
    #  判断集合是否存在
    collist = db . list_collection_names()
    if "user" in collist:  
      print("user 集合已存在!")
    
    # 创建、获取集合
    collection = db.user
    collection = db["user"]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    注意: 在 MongoDB 中,数据库创建后要创建集合(数据表)并插入一个文档(记录),数据库才会真正创建。

    新增

    MongoDB 中的一个文档类似 SQL 表中的一条记录。

    单条插入

    集合中插入一个文档使用 insert_one() 方法,该方法的第一参数是字典

    result = collection.insert_one({"name": "Python", "age": 25})
    # 插入操作是否被确认
    print(result.acknowledged)  # True
    # 插入文档的 _id 字段的值。如果文档没有设置 _id 字段,则 inserted_id 的值为一个新生成的 ObjectId。
    print(result.inserted_id)  # 63f569b89a8014989e3b53f6
    
    # 批量插入
    result = collection.insert_many([{"name": "Java", "age": 55}, {"name": "Vue", "age": 33}])
    # 插入操作是否被确认
    print(result.acknowledged)  # True
    # 插入的所有文档的 _id 值的列表。
    print(result.inserted_ids)  # [ObjectId('63f569b89a8014989e3b53f7'), ObjectId('63f569b89a8014989e3b53f8')]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    批量插入

    集合中插入多个文档使用 insert_many() 方法,该方法的第一参数是字典列表

    result = collection.insert_many([{"name": "Java", "age": 55}, {"name": "Vue", "age": 33}])
    # 插入操作是否被确认
    print(result.acknowledged)  # True
    # 插入的所有文档的 _id 值的列表。
    print(result.inserted_ids)  # [ObjectId('63f569b89a8014989e3b53f7'), ObjectId('63f569b89a8014989e3b53f8')]
    
    • 1
    • 2
    • 3
    • 4
    • 5

    查询

    MongoDB 中使用了 find 和 find_one 方法来查询集合中的数据,它类似于 SQL 中的 SELECT 语句。

    查询单条

    使用 find_one()方法来查询集合中的一条数据

    result = collection.find_one({"name": "Python"})
    print(result)
    
    • 1
    • 2

    查询多条

    使用find() 方法查询集合中的所有数据

    rows = collection.find({"name": "Python"})
    for row in rows:
        print(row)
    
    • 1
    • 2
    • 3

    查询指定字段的数据

    使用 find() 方法来查询指定字段的数据,将要返回的字段对应值设置为 1

    注意:除了 _id,不能在一个对象中同时指定 0 和 1

    rows = collection.find({},{ "_id": 0, "name": 1, "age": 1 }):
    for x in 
      print(x)
    
    • 1
    • 2
    • 3

    根据指定条件查询

    可以在 find() 中设置参数来过滤数据

    query = { "name": "java" }
    result = collection.find(query)
    for document in result:
        print(document)
    
    • 1
    • 2
    • 3
    • 4

    高级查询

    查询条件语句中使用修饰符

    # 查询年龄大于等于30
    query = { "age": { "$gte": 30 } }
    result = collection.find(query)
    for document in result:
        print(document)
    
    • 1
    • 2
    • 3
    • 4
    • 5

    使用正则表达式查询

    可以使用正则表达式作为修饰符

    # 查看name 字段中以字母'java'开头的数据
    query = { "name": { "$regex": "^java" } }
    result = collection.find(query)
    for document in result:
        print(document)
    
    • 1
    • 2
    • 3
    • 4
    • 5

    返回指定条数记录

    要对查询结果设置指定条数的记录可以使用 limit() 方法,该方法只接受一个数字参数

    result = collection.find().limit(3)
    for x in result:
      print(x)
    
    • 1
    • 2
    • 3

    查询选择器

    比较操作符

    符号描述示例
    $eq匹配等于指定值的值{‘age’: {‘$eq’: 20}}
    $lt匹配小于指定值的值{‘age’: {‘$lt’: 20}}
    $lte匹配小于或等于指定值的值{‘age’: {‘$lte’: 20}}
    $gt匹配大于指定值的值{‘age’: {‘$gt’: 20}}
    $gte匹配大于或等于指定值的值{‘age’: {‘$gte’: 20}}
    $ne匹配所有不等于指定值的值{‘age’: {‘$ne’: 20}}
    $in匹配数组中指定的任何值{‘age’: {‘$in’: [20, 30]}}
    $nin不匹配数组中指定的任何值{‘age’: {‘$nin’: [20, 30]}}

    逻辑操作符

    符号描述示例说明
    $and查询同时满足多个条件的文档query = { "$and": [ {"name": "electronics"}, {"age": {"$lt": 30}} ] }

    result = collection.find(query)
    查询了name为"electronics"且age小于30的数据
    $not查询不满足指定条件的文档query = { "age": { "$not": {"$gt": 50} # 不大于50 } }查询了age不大于50的数据
    $nor查询不满足任何指定条件的文档query = { "$nor": [ {"name": "electronics"}, {"age": {"$gt": 50}} ] }查询name既不属于"electronics",也不满足age大于50的数据
    $or查询满足任意一个指定条件的文档query = { "$or": [ {"name": "electronics"}, {"age": {"$gt": 50}} ] }查询了name满足"electronics"或age大于50的数据

    其他常见操作符

    符号描述示例
    $regex匹配正则表达式{‘name’: {‘$regex’: ‘^java.*’}}
    $exists属性是否存在{‘name’: {‘$exists’: True}}
    $text文本查询{'$text': {‘$search’: ‘java’}}
    $where高级条件查询{‘$where’: ‘this.name.length > 5’}
    $type类型判断{‘age’: {‘$type’: ‘int’}}
    $mod对字段进行取模运算

    这里:5是除数,0是余数
    {‘age’: {‘$mod’: [5, 0]}}

    更新

    更新单条

    使用 update_one() 方法修改文档中的记录,方法第一个参数为查询的条件,第二个参数为要修改的字段。

    如果查找到的匹配数据多于一条,则只会修改第一条

    '''
    matched_count:符合筛选条件的文档数
    modified_count:实际被更新的文档数
    upserted_id:如果执行的是 upsert 操作,则包含新插入文档的 _id 值
    '''
    result = collection.update_one({"name": "Python"}, {"$set": {"age": 25}})
    print(result.matched_count)
    print(result.modified_count)
    print(result.upserted_id)
    
    
    # 不存在就插入
    data = {"name": "Python", "age": 25}
    client.test.test.update_one({'name': 'Java'}, {'$set': data}, upsert=True)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    批量更新

    使用update_many()方法修改多个文档中的记录,会修改所有匹配到的记录

    collection.update_many({"name": "Python"}, {"$set": {"age": 22}})
    
    # 不存在就插入
    data = {"name": "Python", "age": 25}
    client.test.test.update_one({'name': 'Java'}, {'$set': data}, upsert=True)
    
    • 1
    • 2
    • 3
    • 4
    • 5

    删除

    删除文档

    使用 delete_one() 方法来删除一个文档,该方法第一个参数为查询对象,指定要删除哪些数据

    '''
    acknowledged:一个布尔值,表示操作是否被确认(即是否执行成功)
    deleted_count:一个整数,表示被删除的文档数量
    '''
    result = collection.delete_one({"name": "Python"})
    print(result.acknowledged)
    print(result.deleted_count)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    批量删除

    使用 delete_many() 方法来删除多个文档,该方法第一个参数为查询对象,指定要删除哪些数据

    collection.delete_many({"age": {"$gt": 20}})
    
    • 1

    删除集合中的所有文档

    delete_many() 方法如果传入的是一个空的查询对象,则会删除集合中的所有文档

    x = user.delete_many({})
    print(x.deleted_count, "个文档已删除")
    
    db.user.delete_many({})
    
    • 1
    • 2
    • 3
    • 4

    删除集合

    使用drop()方法来删除一个集合。

    # 返回true:删除成功
    # 返回false:删除失败(集合不存在)
    collection.drop()
    
    • 1
    • 2
    • 3

    其他

    排序

    sort()方法可以指定升序或降序排序。

    第一个参数为要排序的字段,第二个字段指定排序规则,1为升序,-1为降序,默认为升序。

    升序

    result = collection.find().sort("age")
    for x in result:
      print(x)
    
    • 1
    • 2
    • 3

    降序

    result = collection.find().sort("age", -1)
    for x in result:
      print(x)
    
    • 1
    • 2
    • 3

    计数

    可以使用count()方法查询结果有多少条数据

    count = collection.find().count()
    
    query = { "age": { "$gte": 30 } }
    count = collection.find(query).count()
    
    • 1
    • 2
    • 3
    • 4

    分组查询

    在MongoDB中,可以使用聚合管道(aggregation pipeline)来实现分组查询

    # 构建聚合管道
    pipeline = [
        {"$group": {"_id": "$category", "total_sales": {"$sum": "$amount"}}},
        {"$sort": {"total_sales": -1}},
        {"$limit": 3}
    ]
    
    # 执行聚合查询
    result = collection.aggregate(pipeline)
    for document in result:
        print(document)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
  • 相关阅读:
    神经网络与深度学习
    元宇宙带来的游戏变革会是怎样的?
    【历史上的今天】11 月 7 日:图灵奖女性得主诞生;Twitter 告别 140 字符时代;首位中国 AI 主播
    短视频/直播+教育成为教育新常态
    你想对构造函数说些什么?
    LAGRANGIAN FLUID SIMULATION WITH CONTINUOUS CONVOLUTIONS
    STM32H5开发(4)----开发板介绍
    猿创征文 | 【STM32】ESP8266 wifi模块创建阿里云产品
    Centos 7.9安装PostgreSQL14.4步骤
    Java堆外缓存(一个很有意思的应用)
  • 原文地址:https://blog.csdn.net/qq_38628046/article/details/129171208