zoukankan      html  css  js  c++  java
  • mongdb基本使用

    mongodb创建用户,设置密码

    参考:https://www.jianshu.com/p/237a0c5ad9fa

    MongoDB内置的数据库角色有:
    1. 数据库用户角色:read、readWrite;
    2. 数据库管理角色:dbAdmin、dbOwner、userAdmin;
    3. 集群管理角色:clusterAdmin、clusterManager、clusterMonitor、hostManager;
    4. 备份恢复角色:backup、restore;
    5. 所有数据库角色:readAnyDatabase、readWriteAnyDatabase、userAdminAnyDatabase、dbAdminAnyDatabase
    6. 超级用户角色:root
    7. 内部角色:__system
    引用自: https://www.cnblogs.com/qk2014/p/9253506.html
    

    mongodb基本使用

    use DATABASE_NAME
    如果数据库不存在,则创建数据库,否则切换到数据库。
    
    show dbs
    显示所有的数据库
    
    db.dropDatebase()
    删除当前所使用的数据库
    
    db.createCollection(name,options)
    创建名为name的集合
    
    show collections
    显示所有的集合
    
    db.collection.drop()
    删除名为collection的集合
    
    db.COLLECTION_NAME.insert(document)
    向集合COLLECTION_NAME中插入document
    
    db.COLLECTION_NAME.insertOne(document)
    向集合COLLECTION_NAME中插入一条document
    
    db.COLLECTION_NAME.insertMany([document1,document2...])
    向集合COLLECTION_NAME中插入多条document
    
    db.COLLECTION_NAME.update(<filter>,<update>,{...参数})
    更新COLLECTION_NAME中的记录
    filter:过滤条件,update:需要更新的数据
    参数:
    	upsert:可选,如果不存在update的记录,是否插入objNew,true为插入,false为不插入
    	multi:可选,只更新第一条记录,如果这个参数为true,则全部更新,为false则只更新一条数据,mongodb默认为false
    	writeConcern:可选,抛出异常的级别
    
    db.COLLECTION_NAME.delateOne(<filter>)
    删除COLLECTION_NAME集合中的一条记录
    filter:过滤条件
    
    db.COLLECTION_NAME.delateMany(<filter>)
    删除COLLECTION_NAME集合中的多条记录
    filter:过滤条件
    
    db.COLLECTION_NAME.findOne(<filter>)
    查询COLLECTION_NAME集合中的一条记录
    filter:过滤条件
    
    db.COLLECTION_NAME.findMany(<filter>)
    查询COLLECTION_NAME集合中的多条记录
    filter:过滤条件
    
    db.user.find({"name":"/帅/"})
    在user集合中查找name中包含"帅"的记录
    db.user.find({"name":"/^帅/"})
    在user集合中查找name中开头为"帅"的记录
    db.user.find({"name":"/帅$/"})
    在user集合中查找name中结尾为"帅"的记录
    

    条件语句查询

    操作 格式 案例
    等于 {key: value} db.user.find({"name": "yven"})
    小于 {key: {$lt: value}} db.user.find({"age": {$lt: 20}})
    小于或等于 {key: {$lte: value}} db.user.find({"age": {$lte: 20}})
    大于 {key: {$gt: value}} db.user.find({"age": {$gt: 20}})
    大于或等于 {key: {$gte: value}} db.user.find({"age": {$gte: 20}})
    不等于 {key: {$ne: value}} db.user.find({"age": {$ne:20}})

    AND和OR

    在find方法中传入多个键,每个键以逗号隔开,就是AND条件,如:
    db.user.find({"name":"yven","age",18})
    在user集合中查找name为yven而且age为18的记录
    
    OR条件使用关键字$or,如:
    db.user.find({"$or":[{"name":"yven"},{"name":"lyf"}]})
    在user集合中查找name为yven或者name为lyf的记录
    

    $type实例

    $type操作符是基于BSON类型来检索集合中匹配的数据类型,并返回结果

    MongoDB中可以使用的类型参考如下:

    https://www.runoob.com/mongodb/mongodb-operators-type.html

    使用$type实例

    db.user.find({"name":{$type:2}})
    $type 为2对应的数据类型为string,所以这条语句就是在user集合中查询name为string类型的记录
    也可以写为:db.user.find({"name":{$type:"string"}})
    

    pymongodb基本使用

    from pymongo import MongoClient
    mongodb_add = "mongodb://user:pwd@host:port"
    client = MongoClient(mongodb_add)
    # 获取数据库
    db = client["datebase_name"]
    # 获取集合
    collection = db["collection_name"]
    
    # 操作就完事
    yven = collection.find_one({"name":"yven"})
    # 模糊查询,查看name中包含y的记录条数
    count = collection.find({"name": {'$regex': ".*y.*"}}).count()
    
  • 相关阅读:
    (转)Android之内存泄漏调试学习与总结
    (转)linux下bluetooth编程(七)SDP协议
    (转)Linux下Bluez的编程实现
    (转)linux下bluetooth编程(八)SDP层编程
    (转)linux下bluetooth编程(二)blueZ协议栈
    (转)linux下bluetooth编程(六)L2CAP层编程实例
    (转)linux下bluetooth编程(五)bluetooth与socket
    (转)linux下bluetooth编程(四)L2CAP层编程
    (转)linux下bluetooth编程(一)基础概念
    (转)linux下bluetooth编程(三)HCI层编程
  • 原文地址:https://www.cnblogs.com/louyefeng/p/12031261.html
Copyright © 2011-2022 走看看