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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM