1. 程式人生 > >MongoDB 命令 (MongoDB Shell)

MongoDB 命令 (MongoDB Shell)

年齡 基本 update upd div 計數 fin eid 使用

1、我們 mongodb 安裝成功後,用上一篇的方法啟動 mongodb服務 然後使用 mongodb shell 來做數據庫的增刪改查

2、創建數據庫

  語法:

use 數據庫名稱

  案例:

> use juyou
switched to db juyou
> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB

  這時創建完成過,使用命令查詢數據庫卻沒有我們剛創建的數據庫,這時因為剛創建的數據庫沒有數據,下面我們在數據庫中插入一條數據

> db.juyou.insert({"name":"聚優福利"})
WriteResult({ 
"nInserted" : 1 }) > show dbs admin 0.000GB config 0.000GB juyou 0.000GB local 0.000GB

  這時就能看到剛剛創建的數據庫了

3、刪除數據庫

  語法:

db.dropDatabase()

  案例:

  首先我們先查詢一下所有的數據庫

> show dbs
admin   0.000GB
config  0.000GB
juyou   0.000GB
local   0.000GB

  然後我們可以使用 db 來查看當前的數據庫

> db
juyou

  當前鏈接的數據庫就是 juyou,如果不是可以使用 use juyou 命令切換到 juyou 數據庫

> use juyou
switched to db juyou

  執行刪除命令

> db.dropDatabase()
{ "dropped" : "juyou", "ok" : 1 }

  然後再我們再查詢一下所有數據庫

> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB

  已經成功刪除了

4、創建集合

  語法:

db.createCollection(name, options)

  name:集合名稱

  options: 可選參數 

  案例:創建一個名為 userinfo 的集合

> db.createCollection("
userinfo") { "ok" : 1 } > show collections userinfo

  創建成功後可以使用 show collections 命令查詢已有集合

5、插入文檔

  語法:

db.集合名稱.insert(document)

  案例:

  在 juyou 集合下的 userinfo 文檔中插入一條數據

> db.juyou.userinfo.insert({name:"郭大爺","sex":"","age":"不詳"})
WriteResult({ "nInserted" : 1 })
> db.juyou.userinfo.find()
{ "_id" : ObjectId("5abaf679a3aadbe625070c4f"), "name" : "郭大爺", "sex" : "", "age" : "不詳" }

  插入成功後,可以使用 find() 來查詢剛剛插入的數據,下面會對查詢做詳細的講解,這裏不多做解釋

  可以看到插入數據後,多了一列 _id 的數據,在文檔中 mongodb 會將 _id 字段自動設置為主鍵,如果不指定mongodb會自動生成

  自動生成的 ObjectId 是由時間戳、MachineID(電腦的 mac 地址)、進程ID以及自增計數器組成的,基本上不會重復

> db.juyou.userinfo.insert({"_id":1,name:"郭少爺","sex":"","age":"不詳"})
WriteResult({ "nInserted" : 1 })
> db.juyou.userinfo.find()
{ "_id" : ObjectId("5abaf679a3aadbe625070c4f"), "name" : "郭大爺", "sex" : "", "age" : "不詳" }
{ "_id" : 1, "name" : "郭少爺", "sex" : "", "age" : "不詳" }

  也可以在插入數據時指定 _id 值,在之前使用mongodb開發中會指定給 _id 值,使用GUID(全球唯一標識)代替

  我們也可以先將要插入的數據定義成變量

> var user = {name:"郭老師",sex:"",age:"18"}
> db.juyou.userinfo.insert(user)
WriteResult({ "nInserted" : 1 })
> db.juyou.userinfo.find()
{ "_id" : ObjectId("5abaf679a3aadbe625070c4f"), "name" : "郭大爺", "sex" : "", "age" : "不詳" }
{ "_id" : 1, "name" : "郭少爺", "sex" : "", "age" : "不詳" }
{ "_id" : ObjectId("5abb05afa3aadbe625070c50"), "name" : "郭老師", "sex" : "", "age" : "18" }

  mongodb 在3.2版本後 提供了一次插入多條數據的方法 insertMany() ,我們下面把上面的三條數據刪除,然後試一下一次插入多條數據

> db.juyou.userinfo.remove({})
WriteResult({ "nRemoved" : 3 })
> db.juyou.userinfo.find()
> var users = [
...   {
...     _id:1,
...     name:"郭大爺",
...     sex:"",
...     age:"80"
...   },
...   {
...     _id:2,
...     name:"郭老師",
...     sex:"",
...     age:"不詳"
...   },
...   {
...     _id:3,
...     name:"郭少爺",
...     sex:"",
...     age:"18"
...   }
... ]
> db.juyou.userinfo.insertMany(users)
{ "acknowledged" : true, "insertedIds" : [ 1, 2, 3 ] }
> db.juyou.userinfo.find()
{ "_id" : 1, "name" : "郭大爺", "sex" : "", "age" : "80" }
{ "_id" : 2, "name" : "郭老師", "sex" : "", "age" : "不詳" }
{ "_id" : 3, "name" : "郭少爺", "sex" : "", "age" : "18" }

  這樣我們可以直接插入一個數組

6、更新文檔

  語法:

db.collection.update(
   <query>,
   <update>,
   {
     upsert: <boolean>,
     multi: <boolean>,
     writeConcern: <document>
   }
)

  query:條件,相當於sql update時where條件

  update: 要更新的內容,類似 sql 的 set 後面的內容

  案例:我們先查詢一下郭老師的年齡是不詳,現在我們根據主鍵_id來把年齡更新成20歲

> db.juyou.userinfo.find()
{ "_id" : 1, "name" : "郭大爺", "sex" : "", "age" : "80" }
{ "_id" : 2, "name" : "郭老師", "sex" : "", "age" : "不詳" }
{ "_id" : 3, "name" : "郭少爺", "sex" : "", "age" : "18" }
> db.juyou.userinfo.update({"_id":2},{$set:{"age":"20"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.juyou.userinfo.find()
{ "_id" : 1, "name" : "郭大爺", "sex" : "", "age" : "80" }
{ "_id" : 2, "name" : "郭老師", "sex" : "", "age" : "20" }
{ "_id" : 3, "name" : "郭少爺", "sex" : "", "age" : "18" }

  已經成功將郭老師的年齡改成20,然後我們看到在更新命令中又一個 $set 的關鍵詞,這個是更新操作符,下面來介紹一下

  

MongoDB 命令 (MongoDB Shell)