您好,登錄后才能下訂單哦!
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
歡迎關注我的微信公眾號"IT小Chen",共同學習,共同成長!!!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。