zoukankan      html  css  js  c++  java
  • MongoDB入门(4)- MongoDB日常操作

    MongoDB客户端

    MongoDB有很多客户端
    MongoVue
    Robomongo

    MongoDB命令行

    启动mongo shell

    在windows下,双击mongo.exe可以启动mongo shell

    查询库、表及选择库

    查询所有库命令:

    show dbs
    

    应用某一个db

    use jxs_database
    

    查询此db里面所有collection

    show collections
    

    查询数据

    去重查询

    db.getCollection('MonitorInfo').distinct("Level")
    

    查询所有数据

    db.asset_entity.find()
    

    查询一条数据

    db.asset_entity.findOne()
    

    查询条数

    db.asset_entity.find()
    

    查询某一条符合条件的数据

    db.asset_entity.find({"voucher_number":"5555"})
    

    只查询某一列数据

    db.asset_entity.find({},{"change_time":true})
    
    db.asset_entity.findOne({"voucher_number":"444345"})
    

    查询符合条件的某N列数据

    db.asset_entity.find({"voucher_number":"5555"},{"change_time":true,})
    
    db.asset_entity.find({"voucher_number":"5555"},{"change_time":true,"voucher_number":true})
    

    查询在18~30岁(含)的用户

    db.users.find({"age" : {"$gte" : 18, "$lte" : 30}})
    

    要查找在2007年1月1日前注册的人,可以像下面这样:

    >start = new Date("01/01/2007")
    >db.users.find({"registered" : {"$lt" : start}})
    

    查找排序

    db.getCollection('ReportLog').find({}).sort({"createtime":1})
    

    like查找

    db.getCollection('ReportClientMongoLog').find({"Msg":/.*工作.*/}).count()
    
    db.getCollection('ReportClientMongoLog').find({"Msg":/.*闲置.*/}).count()
    
    db.getCollection('SalaryEntity').find({"Month" : "201601"})
    
    db.getCollection('SalaryEntity').find({"Month" : "201601", "DepartName" : "运维与管理软件部"})
    
    db.getCollection('ReportClientMongoLog').find({"Ip":"192.168.88.136"},{"CreateTime":true}).sort({"CreateTime":-1})
    
    db.getCollection('ReportClientMongoLog').find().count()
    
    db.getCollection('SalaryEntity').find({"Month" : "201601"},{"DepartName":true})
    

    删除数据

    删除符合条件的数据

    db.asset_entity.remove({"voucher_number":"5555"})
    
    db.getCollection('MyVersion').remove({})
    
    db.getCollection('ReportHeartBeatKey').remove({"Ip":/.*88.*/})
    

    更新数据

    db.asset_check.update({"asset_num":"NUM19"},{"$set":{"model":"x230i"}},false,true)
    
    如果没有后面两个参数,则只更新一行数据。
    
    db.getCollection('PersonBaseInfo').update(
        // query 
        {
            "DepartName" : "MIS与互联网部"
        },
        
        // update 
        {
            "$set":
            {        
                "DepartName" : "互联网与无线电项目部"
            }
        },
        
        // options 
        {
            "multi" : true,  // update only one document 
            "upsert" : false  // insert a new document, if no existing document match the query 
        }
    );
    

    插入数据

    插入一条数据

    db.asset_type.insert({"serialId":"161261","name":"mytest","pid":"16126"})
    

    更改表结构

    mongo里面没有表结构这个概念,现在采用类似关系型数据库的形式来描述。如果想去掉collection里面的一个key,可以采用以下命令:

    db.UserEntity.update({},{$unset:{Mail:1}},false,true);
    

    上面的命令从表UserEntity中删除一个字段Mail。
    关于unset的具体说明

    $unset
    The $unset operator deletes a particular field. Consider the following syntax:
    
    { $unset: { <field1>: "", ... } }
    The specified value in the $unset expression (i.e. "") does not impact the operation.
    
    To specify a <field> in an embedded document or in an array, use dot notation.
    
    Behavior
    
    If the field does not exist, then $unset does nothing (i.e. no operation).
    
    When used with $ to match an array element, $unset replaces the matching element with null rather than removing the matching element from the array. This behavior keeps consistent the array size and element positions.
    
    Example
    
    The following update() operation uses the $unset operator to remove the fields quantity and instock from the first document in the products collection where the field sku has a value of unknown.
    
    db.products.update(
       { sku: "unknown" },
       { $unset: { quantity: "", instock: "" } }
    )
    SEE ALSO
    
  • 相关阅读:
    python 编码与解码
    python 写文件
    python 文件读写
    python 异常处理
    python 断言
    C++的可移植性和跨平台开发
    Python中subprocess学习
    Python 的web自动化测试
    CookieJar和HTTPCookieProcessor
    python3爬虫
  • 原文地址:https://www.cnblogs.com/wardensky/p/5799283.html
Copyright © 2011-2022 走看看