MongoDB常用命令彙總(一)

chenoracle發表於2018-11-27

MongoDB常用命令彙總(一)

---增,刪,改,查


一:增(insert)

二:刪(delete)

三:改(update)

四:查(select) 


---連線資料庫

[mongo@cjc bin]$ pwd

/usr/local/mongodb/bin

[mongo@cjc bin]$ ./mongo --port 27017

---建立新資料庫cjcdb和cjctest

MongoDB Enterprise > use cjcdb

---插入資料,才會建立資料庫

MongoDB Enterprise > db.tab01.insert({"time":"2018-11-25 18:20"})

MongoDB Enterprise > use cjctest

MongoDB Enterprise > db.tab01.insert({"time":"2018-11-25 18:21"})

---檢視有哪些資料庫

MongoDB Enterprise > show dbs

...

cjcdb           0.000GB

cjctest         0.000GB

---MongoDB 刪除資料庫cjctest

MongoDB Enterprise > use cjctest

switched to db cjctest

---檢視當前連線的資料庫

MongoDB Enterprise > db

cjctest

---刪除資料庫(謹慎操作)

MongoDB Enterprise > db.dropDatabase()

{ "dropped" : "cjctest", "ok" : 1 }

---MongoDB 建立集合

MongoDB Enterprise > use cjcdb

switched to db cjcdb

MongoDB Enterprise > db.createCollection("tab02")

{ "ok" : 1 }

MongoDB Enterprise > db.createCollection("tab03")

{ "ok" : 1 }

在 MongoDB 中,你不需要建立集合。當你插入一些文件時,MongoDB 會自動建立集合。

MongoDB Enterprise > db.tab05.insert({"id":"123456"})

WriteResult({ "nInserted" : 1 })

MongoDB Enterprise > show collections

tab01

tab02

tab03

tab05

......

================

一:增(insert)

================

插入文件

MongoDB 使用 insert() 或 save() 方法向集合中插入文件:

MongoDB Enterprise > use cjcdb

switched to db cjcdb

MongoDB Enterprise > db

cjcdb

MongoDB Enterprise >

db.tab02.insert({id:1, name:'a'})

db.tab02.insert({id:1, name:'ab'})

db.tab02.insert({id:1, name:'cae'})

db.tab02.insert({id:2, name:'b'})

db.tab02.insert({id:3, name:'c'})

db.tab02.insert({id:4, name:'d'})

db.tab02.insert({id:5, name:'e'})

db.tab02.insert({id:6, name:'f'})

db.tab02.insert({id:7, name:'g'})

db.tab02.insert({id:8, name:'h'})

db.tab02.insert({id:9, name:'i'})

db.tab02.insert({id:10, name:'j'})

db.tab02.insert({id:10, name:'h'})

db.tab02.insert({id:10, name:'i'})

db.tab02.insert({id:10,c100:'h',c200:'f',c300:'業精於勤'})

db.tab02.insert({ccc:10,c10:'h',c20:'f',c30:'行成於思'})

---我們也可以將資料定義為一個變數,如下所示:

MongoDB Enterprise > document=({title: 'blog.itpub', 

...     description: 'chenoracleblog-chenjuchao',

...     by: 'mongo test',

...     url: 'http://blog.itpub.net/29785807/'

... })

MongoDB Enterprise > db.tab02.insert(document)

=================

二:刪(delete)

=================

---delete

MongoDB remove()函式是用來移除集合中的資料。

MongoDB資料更新可以使用update()函式。在執行remove()函式前先執行find()命令來判斷執行的條件是否正確,這是一個比較好的習慣

MongoDB Enterprise > db

cjcdb

---刪除tab02表中id=10的資料

MongoDB Enterprise > db.tab02.find({id:10})

{ "_id" : ObjectId("5bfacc790063f23910737472"), "id" : 10, "name" : "j" }

{ "_id" : ObjectId("5bfacc790063f23910737473"), "id" : 10, "name" : "h" }

{ "_id" : ObjectId("5bfacc790063f23910737474"), "id" : 10, "name" : "i" }

{ "_id" : ObjectId("5bfacc790063f23910737475"), "id" : 10, "c100" : "h", "c200" : "f", "c300" : "業精於勤" }

MongoDB Enterprise > 

MongoDB Enterprise > db.tab02.remove({id:10})

WriteResult({ "nRemoved" : 4 })

---remove() 方法已經過時了,現在官方推薦使用 deleteOne() 和 deleteMany() 方法。

刪除 id 等於 1 的第一個文件:

MongoDB Enterprise > db.tab02.find({id:1})

{ "_id" : ObjectId("5bfacc790063f23910737467"), "id" : 1, "name" : "a" }

{ "_id" : ObjectId("5bfacc790063f23910737468"), "id" : 1, "name" : "ab" }

{ "_id" : ObjectId("5bfacc790063f23910737469"), "id" : 1, "name" : "cae" }

MongoDB Enterprise > db.tab02.deleteOne({id:1})

{ "acknowledged" : true, "deletedCount" : 1 }

MongoDB Enterprise > db.tab02.find({id:1})

{ "_id" : ObjectId("5bfacc790063f23910737468"), "id" : 1, "name" : "ab" }

{ "_id" : ObjectId("5bfacc790063f23910737469"), "id" : 1, "name" : "cae" }

---刪除 id 等於 1 的全部文件:

MongoDB Enterprise > db.tab02.deleteMany({id:1})

{ "acknowledged" : true, "deletedCount" : 2 }

MongoDB Enterprise > db.tab02.find({id:1})

MongoDB Enterprise > 

---如刪除集合下全部文件:

---truncate table ......

MongoDB Enterprise > db.tab02.find()

{ "_id" : ObjectId("5bfacc790063f2391073746a"), "id" : 2, "name" : "b" }

{ "_id" : ObjectId("5bfacc790063f2391073746b"), "id" : 3, "name" : "c" }

{ "_id" : ObjectId("5bfacc790063f2391073746c"), "id" : 4, "name" : "d" }

{ "_id" : ObjectId("5bfacc790063f2391073746d"), "id" : 5, "name" : "e" }

{ "_id" : ObjectId("5bfacc790063f2391073746e"), "id" : 6, "name" : "f" }

{ "_id" : ObjectId("5bfacc790063f2391073746f"), "id" : 7, "name" : "g" }

{ "_id" : ObjectId("5bfacc790063f23910737470"), "id" : 8, "name" : "h" }

{ "_id" : ObjectId("5bfacc790063f23910737471"), "id" : 9, "name" : "i" }

{ "_id" : ObjectId("5bfacc7a0063f23910737476"), "ccc" : 10, "c10" : "h", "c20" : "f", "c30" : "行成於思" }

{ "_id" : ObjectId("5bfaccb10063f23910737477"), "title" : "blog.itpub", "description" : "chenoracleblog", "by" : "mongo test", "url" : "http://blog.itpub.net/29785807/" }

MongoDB Enterprise > db.tab02.deleteMany({})

{ "acknowledged" : true, "deletedCount" : 10 }

MongoDB Enterprise > db.tab02.find()

MongoDB Enterprise > 

---刪除集合

---drop table ......

MongoDB Enterprise > show collections

tab01

tab02

tab03

MongoDB Enterprise > db.tab02.drop()

true

MongoDB Enterprise > show collections

tab01

tab03

===============

三:改(update)

===============

---MongoDB 更新文件

MongoDB 使用 update() 和 save() 方法來更新集合中的文件。

MongoDB Enterprise > db.tab05.find({'id':1})

{ "_id" : ObjectId("5bfafab30063f23910737478"), "id" : 1, "name" : "a" }

{ "_id" : ObjectId("5bfafab30063f23910737479"), "id" : 1, "name" : "aa" }

{ "_id" : ObjectId("5bfafab30063f2391073747a"), "id" : 1, "name" : "aaa" }

---更改符合條件的第一條資料

MongoDB Enterprise > db.tab05.update({'id':1},{$set:{name:'chenjuchao'}})

WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

MongoDB Enterprise > db.tab05.find({'id':1})

{ "_id" : ObjectId("5bfafab30063f23910737478"), "id" : 1, "name" : "chenjuchao" }

{ "_id" : ObjectId("5bfafab30063f23910737479"), "id" : 1, "name" : "aa" }

{ "_id" : ObjectId("5bfafab30063f2391073747a"), "id" : 1, "name" : "aaa" }

---multi:true更改符合條件的所有資料

MongoDB Enterprise > db.tab05.update({'id':1},{$set:{name:'chenjuchao'}},{multi:true})

WriteResult({ "nMatched" : 3, "nUpserted" : 0, "nModified" : 2 })

MongoDB Enterprise > db.tab05.find({'id':1})

{ "_id" : ObjectId("5bfafab30063f23910737478"), "id" : 1, "name" : "chenjuchao" }

{ "_id" : ObjectId("5bfafab30063f23910737479"), "id" : 1, "name" : "chenjuchao" }

{ "_id" : ObjectId("5bfafab30063f2391073747a"), "id" : 1, "name" : "chenjuchao" }

================

四:查(select)

================

1 (!=)不等於 - $ne

2 (in)包含 - $in

3 (not in) - $nin 不包含

4 (>) 大於 - $gt

5 (>=) 大於等於 - $gte

6 (<) 小於 - $lt

7 (<= ) 小於等於 - $lte

8 模糊查詢

9 AND

10 OR 

11 Limit

12 Skip

13 Sort

14 count 


---插入測試資料

---以易讀的方式來讀取資料,可以使用 pretty() 方法

MongoDB Enterprise> 

db.tab01.insert({name:'a',age:'10'})

db.tab01.insert({name:'ab',age:'10'})

db.tab01.insert({name:'bac',age:'10'})

db.tab01.insert({name:'d',age:'12'})

db.tab01.insert({name:'e',age:'13'})

db.tab01.insert({name:'aaa',age:'14'})

db.tab01.insert({name:'f',age:'15'})

db.tab01.insert({name:'g',age:'16'})

db.tab01.insert({name:'a',age:'17'})

db.tab01.insert({name:'a',age:'18'})

db.tab01.insert({name:'a',age:19})

db.tab01.insert({name:'a',age:20})

---檢視全部

MongoDB Enterprise> db.tab01.find()

---加pretty()更易讀

db.tab01.find().pretty()

=====

等於

=====

MongoDB Enterprise> db.tab01.find({age:20})

{ "_id" : ObjectId("5bfccbc636850458dff055b0"), "name" : "a", "age" : 20 }

MongoDB Enterprise> db.tab01.find({age:'20'})

MongoDB Enterprise> 

MongoDB Enterprise> db.tab01.find({age:'10'})

{ "_id" : ObjectId("5bfcc7b036850458dff055a5"), "name" : "a", "age" : "10" }

{ "_id" : ObjectId("5bfcc7b036850458dff055a6"), "name" : "ab", "age" : "10" }

{ "_id" : ObjectId("5bfcc7b036850458dff055a7"), "name" : "bac", "age" : "10" }

MongoDB Enterprise> db.tab01.find({age:10})

MongoDB Enterprise>

====================

1 (!=)不等於 - $ne 

====================

MongoDB Enterprise> db.tab01.find({name:{$ne:'a'}});

{ "_id" : ObjectId("5bfcc7b036850458dff055a6"), "name" : "ab", "age" : "10" }

{ "_id" : ObjectId("5bfcc7b036850458dff055a7"), "name" : "bac", "age" : "10" }

{ "_id" : ObjectId("5bfcc7b036850458dff055a8"), "name" : "d", "age" : "12" }

{ "_id" : ObjectId("5bfcc7b036850458dff055a9"), "name" : "e", "age" : "13" }

{ "_id" : ObjectId("5bfcc7b036850458dff055aa"), "name" : "aaa", "age" : "14" }

{ "_id" : ObjectId("5bfcc7b036850458dff055ab"), "name" : "f", "age" : "15" }

{ "_id" : ObjectId("5bfcc7b036850458dff055ac"), "name" : "g", "age" : "16" }

{ "_id" : ObjectId("5bfcf37936850458dff055b1"), "name" : "nba", "age" : 21 }

{ "_id" : ObjectId("5bfcf37936850458dff055b2"), "name" : "cba", "age" : 21 }

==================

2 (in)包含 - $in

==================

MongoDB Enterprise> db.tab01.find({name:{$in:['cba','nba']}});

{ "_id" : ObjectId("5bfcf37936850458dff055b1"), "name" : "nba", "age" : 21 }

{ "_id" : ObjectId("5bfcf37936850458dff055b2"), "name" : "cba", "age" : 21 }

=========================

3 (not in) - $nin 不包含

=========================

MongoDB Enterprise> db.tab01.find({age:{$nin:['10','12','13','14','15',21]}});

{ "_id" : ObjectId("5bfcc7b036850458dff055ac"), "name" : "g", "age" : "16" }

{ "_id" : ObjectId("5bfcc7b036850458dff055ad"), "name" : "a", "age" : "17" }

{ "_id" : ObjectId("5bfcc7b136850458dff055ae"), "name" : "a", "age" : "18" }

{ "_id" : ObjectId("5bfccbc536850458dff055af"), "name" : "a", "age" : 19 }

{ "_id" : ObjectId("5bfccbc636850458dff055b0"), "name" : "a", "age" : 20 }

=================

4 (>) 大於 - $gt 

=================

MongoDB Enterprise> db.tab01.find({age:{$gt:'17'}})

{ "_id" : ObjectId("5bfcc7b136850458dff055ae"), "name" : "a", "age" : "18" }

========================

5 (>=) 大於等於 - $gte

========================

MongoDB Enterprise> db.tab01.find({age:{$gte:'17'}})

{ "_id" : ObjectId("5bfcc7b036850458dff055ad"), "name" : "a", "age" : "17" }

{ "_id" : ObjectId("5bfcc7b136850458dff055ae"), "name" : "a", "age" : "18" }

========================

6 (<) 小於 - $lt

========================

MongoDB Enterprise> db.tab01.find({age:{$lt:'12'}})

{ "_id" : ObjectId("5bfcc7b036850458dff055a5"), "name" : "a", "age" : "10" }

{ "_id" : ObjectId("5bfcc7b036850458dff055a6"), "name" : "ab", "age" : "10" }

{ "_id" : ObjectId("5bfcc7b036850458dff055a7"), "name" : "bac", "age" : "10" }

========================

7 (<= ) 小於等於 - $lte

========================

MongoDB Enterprise> db.tab01.find({age:{$lte:'12'}})

{ "_id" : ObjectId("5bfcc7b036850458dff055a5"), "name" : "a", "age" : "10" }

{ "_id" : ObjectId("5bfcc7b036850458dff055a6"), "name" : "ab", "age" : "10" }

{ "_id" : ObjectId("5bfcc7b036850458dff055a7"), "name" : "bac", "age" : "10" }

{ "_id" : ObjectId("5bfcc7b036850458dff055a8"), "name" : "d", "age" : "12" }

============

8 模糊查詢

============

---查詢 name 包含"a"的文件:

MongoDB Enterprise> db.tab01.find({name:/a/})

{ "_id" : ObjectId("5bfcc7b036850458dff055a5"), "name" : "a", "age" : "10" }

{ "_id" : ObjectId("5bfcc7b036850458dff055a6"), "name" : "ab", "age" : "10" }

{ "_id" : ObjectId("5bfcc7b036850458dff055a7"), "name" : "bac", "age" : "10" }

{ "_id" : ObjectId("5bfcc7b036850458dff055aa"), "name" : "aaa", "age" : "14" }

{ "_id" : ObjectId("5bfcc7b036850458dff055ad"), "name" : "a", "age" : "17" }

{ "_id" : ObjectId("5bfcc7b136850458dff055ae"), "name" : "a", "age" : "18" }

{ "_id" : ObjectId("5bfccbc536850458dff055af"), "name" : "a", "age" : 19 }

{ "_id" : ObjectId("5bfccbc636850458dff055b0"), "name" : "a", "age" : 20 }

---查詢 name 欄位以"a"開頭的文件:

MongoDB Enterprise> db.tab01.find({name:/^a/})

{ "_id" : ObjectId("5bfcc7b036850458dff055a5"), "name" : "a", "age" : "10" }

{ "_id" : ObjectId("5bfcc7b036850458dff055a6"), "name" : "ab", "age" : "10" }

{ "_id" : ObjectId("5bfcc7b036850458dff055aa"), "name" : "aaa", "age" : "14" }

{ "_id" : ObjectId("5bfcc7b036850458dff055ad"), "name" : "a", "age" : "17" }

{ "_id" : ObjectId("5bfcc7b136850458dff055ae"), "name" : "a", "age" : "18" }

{ "_id" : ObjectId("5bfccbc536850458dff055af"), "name" : "a", "age" : 19 }

{ "_id" : ObjectId("5bfccbc636850458dff055b0"), "name" : "a", "age" : 20 }

---查詢 name 欄位以"a"結尾的文件:

---db.tab01.insert({name:'nba',age:21})

---db.tab01.insert({name:'cba',age:21})

MongoDB Enterprise> db.tab01.find({name:/a$/})

{ "_id" : ObjectId("5bfcc7b036850458dff055a5"), "name" : "a", "age" : "10" }

{ "_id" : ObjectId("5bfcc7b036850458dff055aa"), "name" : "aaa", "age" : "14" }

{ "_id" : ObjectId("5bfcc7b036850458dff055ad"), "name" : "a", "age" : "17" }

{ "_id" : ObjectId("5bfcc7b136850458dff055ae"), "name" : "a", "age" : "18" }

{ "_id" : ObjectId("5bfccbc536850458dff055af"), "name" : "a", "age" : 19 }

{ "_id" : ObjectId("5bfccbc636850458dff055b0"), "name" : "a", "age" : 20 }

{ "_id" : ObjectId("5bfcf37936850458dff055b1"), "name" : "nba", "age" : 21 }

{ "_id" : ObjectId("5bfcf37936850458dff055b2"), "name" : "cba", "age" : 21 }

=======

9 AND 

=======

MongoDB 的 find() 方法可以傳入多個鍵(key),每個鍵(key)以逗號隔開,即常規 SQL 的 AND 條件。

語法格式如下:

MongoDB Enterprise> db.tab01.find({name:'a', age:20})

{ "_id" : ObjectId("5bfccbc636850458dff055b0"), "name" : "a", "age" : 20 }

MongoDB Enterprise> db.tab01.find({name:'a',age:{$gt:18}})

{ "_id" : ObjectId("5bfccbc536850458dff055af"), "name" : "a", "age" : 19 }

{ "_id" : ObjectId("5bfccbc636850458dff055b0"), "name" : "a", "age" : 20 }

=======

10 OR 

=======

MongoDB OR 條件語句使用了關鍵字 $or:

MongoDB Enterprise> db.tab01.find({$or:[{name:'nba'},{age:'12'}]})

{ "_id" : ObjectId("5bfcc7b036850458dff055a8"), "name" : "d", "age" : "12" }

{ "_id" : ObjectId("5bfcf37936850458dff055b1"), "name" : "nba", "age" : 21 }

---AND 和 OR 聯合使用

MongoDB Enterprise> db.tab01.find({name:'a',$or:[{age:'18'},{age:20}]})

{ "_id" : ObjectId("5bfcc7b136850458dff055ae"), "name" : "a", "age" : "18" }

{ "_id" : ObjectId("5bfccbc636850458dff055b0"), "name" : "a", "age" : 20 }

==========

11 Limit

==========

MongoDB Limit() 方法

如果你需要在MongoDB中讀取指定數量的資料記錄,可以使用MongoDB的Limit方法,limit()方法接受一個數字引數,該引數指定從MongoDB中讀取的記錄條數。

limit()方法基本語法如下所示:

MongoDB Enterprise> db.tab01.find({age:'10'}).limit(1)

{ "_id" : ObjectId("5bfcc7b036850458dff055a5"), "name" : "a", "age" : "10" }

MongoDB Enterprise> db.tab01.find().limit(2)

{ "_id" : ObjectId("5bfcc7b036850458dff055a5"), "name" : "a", "age" : "10" }

{ "_id" : ObjectId("5bfcc7b036850458dff055a6"), "name" : "ab", "age" : "10" }

=========

12 Skip

=========

MongoDB Skip() 方法

我們除了可以使用limit()方法來讀取指定數量的資料外,還可以使用skip()方法來跳過指定數量的資料,skip方法同樣接受一個數字引數作為跳過的記錄條數。

注:skip()方法預設引數為 0 。

skip() 方法指令碼語法格式如下:

>db.COLLECTION_NAME.find().limit(NUMBER).skip(NUMBER)

例項

以下例項只會顯示第二條文件資料

MongoDB Enterprise> db.tab01.find({age:'10'}).limit(1).skip(2)

{ "_id" : ObjectId("5bfcc7b036850458dff055a7"), "name" : "bac", "age" : "10" }

=======================

13 MongoDB 排序 Sort

=======================

skip(), limilt(), sort()三個放在一起執行的時候,執行的順序是先 sort(), 然後是 skip(),最後是顯示的 limit()。

MongoDB sort() 方法

在 MongoDB 中使用 sort() 方法對資料進行排序,sort() 方法可以通過引數指定排序的欄位,並使用 1 和 -1 來指定排序的方式,其中 1 為升序排列,而 -1 是用於降序排列。

語法

sort()方法基本語法如下所示:

>db.COLLECTION_NAME.find().sort({KEY:1})

例項

---升序

MongoDB Enterprise> db.tab01.find().sort({age:1})

{ "_id" : ObjectId("5bfccbc536850458dff055af"), "name" : "a", "age" : 19 }

{ "_id" : ObjectId("5bfccbc636850458dff055b0"), "name" : "a", "age" : 20 }

{ "_id" : ObjectId("5bfcf37936850458dff055b1"), "name" : "nba", "age" : 21 }

{ "_id" : ObjectId("5bfcf37936850458dff055b2"), "name" : "cba", "age" : 21 }

{ "_id" : ObjectId("5bfcc7b036850458dff055a5"), "name" : "a", "age" : "10" }

{ "_id" : ObjectId("5bfcc7b036850458dff055a8"), "name" : "d", "age" : "12" }

{ "_id" : ObjectId("5bfcc7b036850458dff055a9"), "name" : "e", "age" : "13" }

{ "_id" : ObjectId("5bfcc7b036850458dff055aa"), "name" : "aaa", "age" : "14" }

......

---降序

MongoDB Enterprise> db.tab01.find().sort({age:-1})

{ "_id" : ObjectId("5bfcc7b136850458dff055ae"), "name" : "a", "age" : "18" }

{ "_id" : ObjectId("5bfcc7b036850458dff055ad"), "name" : "a", "age" : "17" }

{ "_id" : ObjectId("5bfcc7b036850458dff055ac"), "name" : "g", "age" : "16" }

......

---取age最大值

MongoDB Enterprise> db.tab01.find().sort({age:-1}).limit(1)

{ "_id" : ObjectId("5bfcc7b136850458dff055ae"), "name" : "a", "age" : "18" }

---取age最二大值

MongoDB Enterprise> db.tab01.find().sort({age:-1}).limit(1).skip(1)

{ "_id" : ObjectId("5bfcc7b036850458dff055ad"), "name" : "a", "age" : "17" }

---取age最三大值

MongoDB Enterprise> db.tab01.find().sort({age:-1}).limit(1).skip(2)

{ "_id" : ObjectId("5bfcc7b036850458dff055ac"), "name" : "g", "age" : "16" }

======================

14 count查詢記錄條數

======================

使用count()方法查詢表中的記錄條數,例如,下面的命令查詢表users的記錄數量:

MongoDB Enterprise> db.tab01.find().count();

14


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29785807/viewspace-2221865/,如需轉載,請註明出處,否則將追究法律責任。

相關文章