zoukankan      html  css  js  c++  java
  • MongoDB的insert

    insert: 插入数据

    简要说明

    MongoDB中不支持库和集合单独的创建,也就是无法创建一个空的集合。

    如果你进入的这个库或是这个集合是一个新的,那么需要在里面添加数据才能进行保留。

    一、基本使用

    1、判断数据库是否存在

    代码如下:

    # coding:utf8
    import pymongo as p
    
    client = p.MongoClient("mongodb://localhost:27017")
    s1 = input("检查数据库是否存在,请输入")
    if s1 in client.list_database_names():
        print("数据库已经存在")
    else:
        print("数据库不存在,可以使用")
    

    client.list_database_names() 列出当前连接中的所有数据库。

    2、判断集合是否存在

    代码如下:检查指定的集合是否在local库里

    # coding:utf8
    import pymongo as p
    
    client = p.MongoClient("mongodb://localhost:27017")
    db = client["local"]
    s = input("请输入检查的集合名")
    if s in db.list_collection_names():
        print("集合已经存在")
    else:
        print("集合不存在,可以使用")

    db.list_collection_names()列出当前数据库下所有的集合

    3、插入单条数据

    我们创建一个新的数据库叫love,里面有一个集合users

    image

    代码如下:

    # coding:utf8
    import pymongo as p
    
    # 链接数据库
    client = p.MongoClient("mongodb://localhost:27017")
    # 进入数据库, 这个数据库和集合其实不存在,我们进入它然后插入一条数据,它就存在了。
    mydb = client["love"]
    student = mydb["users"]
    
    # 插入的数据其实就是字典
    dict1 = {"name": "白起", "age": 20, "height": 170}
    x = student.insert_one(dict1)
    
    # 打印插入数据的id、users集合
    print(x.inserted_id)
    for v in student.find():
        print(v)

    运行结果:

    image

    4、插入多条数据

    代码如下:

    # coding:utf8
    import pymongo as p
    
    # 链接数据库
    client = p.MongoClient("mongodb://localhost:27017")
    # 进入数据库, 这个数据库和集合其实不存在,我们进入它然后插入一条数据,它就存在了。
    mydb = client["love"]
    student = mydb["users"]
    
    # 插入的数据其实就是字典
    list1 = [{"name": "白起", "age": 21, "height": 170},
             {"name": "王翦", "age": 22, "height": 170},
             {"name": "李牧", "age": 23, "height": 170},
             {"name": "廉颇", "age": 24, "height": 170},
             {"name": "孙武", "age": 25, "height": 170}]
    # 插入多条数据
    x = student.insert_many(list1)
    
    # 打印插入数据的id、users集合
    print(x.inserted_ids)
    for v in student.find():
        print(v)

    运行结果:

    image

    5、插入的id是可以自定义的,不设置则系统会自动生成。

    代码如下:

    # coding:utf8
    import pymongo as p
    
    # 链接数据库
    client = p.MongoClient("mongodb://localhost:27017")
    # 进入数据库, 这个数据库和集合其实不存在,我们进入它然后插入一条数据,它就存在了。
    mydb = client["love"]
    student = mydb["users"]
    
    # 插入的数据其实就是字典
    list1 = [{"_id": 1, "name": "白起", "age": 21, "height": 170},
             {"_id": 2, "name": "王翦", "age": 22, "height": 170},
             {"_id": 3, "name": "李牧", "age": 23, "height": 170},
             {"_id": 4, "name": "廉颇", "age": 24, "height": 170},
             {"_id": 5, "name": "孙武", "age": 25, "height": 170}]
    
    # 插入多条数据
    x = student.insert_many(list1)
    
    # 打印插入数据的id、users集合
    print(x.inserted_ids)
    for v in student.find():
        print(v)

    运行结果:

    image


    读书和健身总有一个在路上

  • 相关阅读:
    【CANoe基础】CANoe常用操作
    ZedGraph控件横纵坐标显示中文名(转)
    Winforn中设置ZedGraph曲线图的属性、坐标轴属性、刻度属性(转)
    c# 访问sqlite资源
    混合模式程序集是针对“v2.0.50727”版的运行时生成的,在没有配置其他信息的情况下,无法在 4.0 运行时中加载该程序集。
    ABP框架资源
    vue新建项目一直在downloading template转,最后超时
    vscode(实验)--ABP框架 .net core 版本的安装与运行(vue模板)(转)
    安装-打开-运行-ABP框架(asp.net core 2.X+Vue)运行前端(转)
    VSCode打开Visual Studio的ABP框架项目(转)
  • 原文地址:https://www.cnblogs.com/Renqy/p/12849266.html
Copyright © 2011-2022 走看看