zoukankan      html  css  js  c++  java
  • 【MongoDB详细使用教程】四、python操作MongoDB

    【MongoDB详细使用教程】一、Mac安装MongoDB
    【MongoDB详细使用教程】二、MongoDB基本操作
    【MongoDB详细使用教程】三、高级查询
    【MongoDB详细使用教程】四、python操作MongoDB
    【MongoDB详细使用教程】五、MongoDB的数据库管理

    使用第三方库pymongo来实现python对MongoDB的操作
    pymongo官方文档:https://api.mongodb.com/python/current/tutorial.html

    1、安装pymongo

    pip install 安装pymongo
    

    2、连接数据库

    import pymongo
    
    client = pymongo.MongoClient('localhost', 27017)    # 连接服务器,需要先开启服务
    db = client['mymongo']  # 选择数据库
    data = db.students.find()   # 查询数据,返回一个游标,通过对游标进行遍历来获取每条数据 
    print(db)
    print(data)
    
    
    # 对查询到的数据进行遍历,每一项为一个dict
    for i in data:
        print(i, type(i))
    

    返回结果:

    MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True)
    Database(MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True), 'mymongo')
    <pymongo.cursor.Cursor object at 0x1058e57f0>
    {'_id': ObjectId('5db642b30f98841018f76965'), 'name': 'chen', 'age': 18.0, 'grade': '一年级'} <class 'dict'>
    {'_id': ObjectId('5db642bc0f98841018f76966'), 'name': 'wang', 'age': 19.0, 'grade': '二年级'} <class 'dict'>
    {'_id': ObjectId('5db653920f98841018f7696b'), 'name': 'xu', 'age': 20.0, 'grade': '三年级', 'text': ['女', '研究员']} <class 'dict'>
    {'_id': ObjectId('5db654660f98841018f7696c'), 'name': 'ma', 'age': 20.0, 'grade': '二年级', 'text': ['女', '副教授', '副处长']} <class 'dict'>
    {'_id': ObjectId('5db68d190f98841018f76970'), 'name': 'cheng', 'age': 21.0, 'grade': '四年级'} <class 'dict'>
    {'_id': ObjectId('5db68f6c0f98841018f76971'), 'name': 'cheng', 'age': 22.0, 'grade': '五年级'} <class 'dict'>
    

    3、操作数据库

    python操作mysql和oracle都是通过直接执行sql来完成,
    而对MongoDB的操作是通过pymongo提供的方法来完成的。

    本节不再把语法单独提出,所有"students"字样均为集合名。

    3.1、查

    data_all = db.students.find()   # 查询全部
    data_lim = db.students.find().limit(1)  # 返回第一条
    data_the = db.students.find({"name": "xu"})  # 条件查询(结果只有1条匹配)
    data_one = db.students.find_one()   # 查询一条
    
    
    print(data_all, type(data_all))
    print(data_lim, type(data_lim))
    print(data_the, type(data_the))
    print(data_one, type(data_one))
    

    虽然后3个方法得到的数据都是1条,但只有使用.find_one()时会返回dict,其余返回的都是Cursor(游标),需要遍历才可以得到具体数据。

    <pymongo.cursor.Cursor object at 0x105a04780> <class 'pymongo.cursor.Cursor'>
    <pymongo.cursor.Cursor object at 0x105a047f0> <class 'pymongo.cursor.Cursor'>
    <pymongo.cursor.Cursor object at 0x105a04860> <class 'pymongo.cursor.Cursor'>
    {'_id': ObjectId('5db642b30f98841018f76965'), 'name': 'chen', 'age': 18.0, 'grade': '一年级'} <class 'dict'>
    

    3.2、增

    # 插入单条
    db.students.insert_one({"name": "zuo", "age": 40, "grate": "九年级"})
    
    # 插入多条
    many_data = [{"name": "ding", "age": 40, "grate": "九年级"},
                 {"name": "liao", "age": 42, "grate": "十年级"},
                 {"name": "zhao", "age": 35, "grate": "九年级"}]
    db.students.insert_many(many_data)
    

    3.3、改

    # 修改单条
    db.students.update_one(filter={"name": "zuo"}, update={"$set": {"grate": "十年级"}})
    
    # 修改全部匹配项
    db.students.update_many(filter={"name": "zuo"}, update={"$set": {"grate": "十年级"}})
    
    
    # filter后为条件,update后为修改后值,其中$set为固定语法。
    

    3.4、删

    # 删除单条
    db.students.delete_one({})  # 删除全部数据的第一条
    db.students.delete_one({"name": "zuo"}) # 删除匹配项的第一条
    
    # 删除多条
    db.students.delete_many({"name":"zuo"}) # 删除集合中的全部数据
    db.students.delete_many({"name":"zuo"}) # 删除全部匹配项
    
  • 相关阅读:
    一键编译go文件命令.bat
    安装go语言,配置环境及IDE,只需3步
    Mysql show Status常用参数详解
    LFS:kernel panic VFS: Unable to mount root fs
    LFS: Interface eth0 doesn't exist
    转:Xshell显示找不到匹配的outgoing encryption算法怎么办
    NTP-ntpdate:no server suitable for synchronization found
    linux/module.h: No such file or directory 内核模块编译过程
    ASCII码表完整版
    [Firmware Warn]: GHES: Failed to read error status block address for hardware error source
  • 原文地址:https://www.cnblogs.com/cbowen/p/11755480.html
Copyright © 2011-2022 走看看