zoukankan      html  css  js  c++  java
  • pymongo 一篇文章搞定

    一 安装

    pip install pymongo

    二 python连接mongodb数据库的前提

    • 确保pymongo安装完毕
    • mongodb数据库的服务器端(mongod)必须处于启动状态

    三 连接mongodb

       连接MongoDB我们需要使用PyMongo库里面的MongoClient,一般来说传入MongoDB的IP及端口即可,第一个参数为地址host,
    第二个参数为端口port,端口如果不传默认是27017。
    • 方式一
      client=pymongo.MongoClient(host='127.0.0.1',port=27017)
    • 方式二
      lient = pymongo.MongoClient('mongodb://localhost:27017/')

    四 指定数据库

    • 特点:找不到数据库,自动创建数据库
    • 代码:
      db=client.test #数据库名为test 
        或者 db = client['test']

    五 指定集合

    • 特点:找不到集合,自动创建集合
    • 代码:
      collection=db.student
      或者collection = db['students']

    六 插入数据

    单条数据的插入

    student = {
        'id': '20170101',
        'name': 'Jordan',
        'age': 20,
        'gender': 'male'
    }
    • 方式一:result=collection.insert(student)
    • 方式二:result=collection.insert_one(student) #推荐使用

    多条数据的插入

    student2 = {
        '_id': '2',
        'name': 'Jordan',
        'age': 30,
        'gender': 'male'
    }
    
    student3 = {
        '_id': '3',
        'name': 'Mike',
        'age': 20,
        'gender': 'male'
    }
    • 方式一:result=collection.insert([student2,student3])
    • 方式二:result=collection.insert_many([student2,student3])#推荐使用

    注意:官方推荐使用insert_one()和insert_many()方法将插入单条和多条记录分开。

    七 查询

    #根据系统生成的id查询 
    from
    bson.objectid import ObjectId result = collection.find_one({'_id': ObjectId('593278c115c2602667ec6bae')}) print(result)
    • 1 单条查询

    results = collection.find_one({'age': 20})
    • 2 查询结果返回多条

    result_list=collection.find({'age':20})
    print(result_list)
    for result in result_list:
        print(result['_id'])

    2.2模糊查询

    '''

    符号含义示例
    $lt小于{'age': {'$lt': 20}}
    $gt大于{'age': {'$gt': 20}}
    $lte小于等于{'age': {'$lte': 20}}
    $gte大于等于{'age': {'$gte': 20}}
    $ne不等于{'age': {'$ne': 20}}
    $in在范围内{'age': {'$in': [20, 23]}}
    $nin不在范围内{'age': {'$nin': [20, 23]}}

    '''

    # 如果要查询年龄大于20的数据,则写法如下:
      
    results = collection.find({'age': {'$gt': 20}})

    2.3 正则查询

    """
    符号含义示例示例含义
    $regex匹配正则{'name': {'$regex': '^M.*'}}name以M开头
    $exists属性是否存在{'name': {'$exists': True}}name属性存在
    $type类型判断{'age': {'$type': 'int'}}age的类型为int
    $mod数字模操作{'age': {'$mod': [5, 0]}}年龄模5余0
    $text文本查询{'$text': {'$search': 'Mike'}}text类型的属性中包含Mike字符串
    $where高级条件查询{'$where': 'obj.fans_count == obj.follows_count'}自身粉丝数等于关注数
    """
    # 另外还可以进行正则匹配查询,例如查询名字以M开头的学生数据,示例如下:
      
    results = collection.find({'name': {'$regex': '^M.*'}})

    3 其他

    • count = collection.find().count() #统计查询返回的数量
    • results = collection.find().sort([('age',pymongo.ASCENDING),('name', pymongo.ASCENDING)]) #多条件排序
    • results = collection.find().sort('name', pymongo.ASCENDING).skip(2) #分页提取数据

    八 更新文档

    condition = {'name': 'Kevin'}
    student = collection.find_one(condition)
    student['age'] = 25
    result = collection.update(condition, student)
    #在这里我们将name为Kevin的数据的年龄进行更新,首先指定查询条件,然后将数据查询出来,修改年龄,
    #之后调用update方法将原条件和修改后的数据传入,即可完成数据的更新。
    condition = {'age': {'$gt': 20}}
    result = collection.update_one(condition, {'$inc': {'age': 1}})
    print(result)
    print(result.matched_count, result.modified_count)
    
    # 在这里我们指定查询条件为年龄大于20,然后更新条件为{'$inc': {'age': 1}},执行之后会讲第一条符合条件的
    数据年龄加1。
    #更新多条数据
    condition = {'age': {'$gt': 20}} result = collection.update_many(condition, {'$inc': {'age': 1}}) print(result) print(result.matched_count, result.modified_count)

    九 删除文档

    # 另外依然存在两个新的推荐方法,delete_one()和delete_many()方法,示例如下:
      
    result = collection.delete_one({'name': 'Kevin'})
    print(result)
    print(result.deleted_count)
    result = collection.delete_many({'age': {'$lt': 25}})
    print(result.deleted_count)

    文章推荐

    https://www.cnblogs.com/pyedu/p/10313148.html

  • 相关阅读:
    2.HTML案例二 头条页面
    1.HTML入门
    33.1.网络编程入门
    32.原子性
    【转】风控中的特征评价指标(一)——IV和WOE
    【转】Python调用C语言动态链接库
    基于蒙特卡洛树搜索(MCTS)的多维可加性指标的异常根因定位
    正则表达式全集
    基于ray的分布式机器学习(二)
    基于ray的分布式机器学习(一)
  • 原文地址:https://www.cnblogs.com/angle6-liu/p/10480314.html
Copyright © 2011-2022 走看看