mongoDB的基本使用方法
MongoDB
安裝(烏班圖系統)
apt install mongodb
mongoDB與sql的對比
SQL術語/概念 | MongoDB術語/概念 | 解釋/說明 |
---|---|---|
database | database | 數據庫 |
table | collection | 數據庫表/集合 |
row | document | 數據記錄行/文檔 |
column | field | 數據字段/域 |
index | index | 索引 |
table joins | 表連接,MongoDB不支持 | |
primary key | primary key | 主鍵,MongoDB自動將_id字段設置爲主鍵 |
連接
命令連接: mongo
zj@zj-Lenovo:~$ mongo MongoDB shell version v3.6.3 connecting to: mongodb://127.0.0.1:27017 MongoDB server version: 3.6.3 Server has startup warnings: 2020-06-21T08:27:21.842+0800 I STORAGE [initandlisten] 2020-06-21T08:27:21.842+0800 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine 2020-06-21T08:27:21.842+0800 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/prodnotes-filesystem 2020-06-21T08:27:27.401+0800 I CONTROL [initandlisten] 2020-06-21T08:27:27.401+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database. 2020-06-21T08:27:27.401+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted. 2020-06-21T08:27:27.401+0800 I CONTROL [initandlisten] >
標準 URI 連接語法:
mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]
創建數據庫
use database_name
例如:命令行執行
> use test switched to db test
刪除數據庫
db.dropDatabase()
:刪除當前數據庫
例如:命令行執行
> use test switched to db test > db.dropDatabase() { "dropped" : "test", "ok" : 1 } > show dbs admin 0.000GB config 0.000GB local 0.000GB
創建集合
db.createCollection("name",options)
:先創建集合
> db.createCollection("title") { "ok" : 1 }
db.collection.insert()
:直接創建並插入數據
> db.title.insert({"name":"mongoDB"}) WriteResult({ "nInserted" : 1 }) > db.title.find() { "_id" : ObjectId("5eeec83e9a6da52bb561a3a5"), "name" : "mongoDB" }
刪除集合
db.collection.drop()
> db.title.drop() true
插入文檔
db.collection.insert()
> db.title.insert({"name":"mongoDB"}) WriteResult({ "nInserted" : 1 }) > db.title.find() { "_id" : ObjectId("5eeec83e9a6da52bb561a3a5"), "name" : "mongoDB" }
db.collection.save()
> db.title.save({"name":"mysql"}) WriteResult({ "nInserted" : 1 }) > db.title.find() { "_id" : ObjectId("5eeec83e9a6da52bb561a3a5"), "name" : "mongoDB" } { "_id" : ObjectId("5eeeca769a6da52bb561a3a6"), "name" : "mysql" }
更新文檔
db.collection.update({query},{$set:{query}})
:默認只更新匹配條件的一條記錄,如要更新全部,設置選項multi:true
例如:更新一條記錄
> db.title.find() { "_id" : ObjectId("5eeec83e9a6da52bb561a3a5"), "name" : "mongoDB" } { "_id" : ObjectId("5eeeca769a6da52bb561a3a6"), "name" : "mysql" } { "_id" : ObjectId("5eeecb6e9a6da52bb561a3a7"), "name" : "mysql" } { "_id" : ObjectId("5eeecb719a6da52bb561a3a8"), "name" : "mysql" } > db.title.update({"name":"mysql"},{$set:{"name":"redis"}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.title.find() { "_id" : ObjectId("5eeec83e9a6da52bb561a3a5"), "name" : "mongoDB" } { "_id" : ObjectId("5eeeca769a6da52bb561a3a6"), "name" : "redis" } { "_id" : ObjectId("5eeecb6e9a6da52bb561a3a7"), "name" : "mysql" } { "_id" : ObjectId("5eeecb719a6da52bb561a3a8"), "name" : "mysql" }
更新多條
> db.title.find() { "_id" : ObjectId("5eeec83e9a6da52bb561a3a5"), "name" : "mongoDB" } { "_id" : ObjectId("5eeeca769a6da52bb561a3a6"), "name" : "redis" } { "_id" : ObjectId("5eeecb6e9a6da52bb561a3a7"), "name" : "mysql" } { "_id" : ObjectId("5eeecb719a6da52bb561a3a8"), "name" : "mysql" } > db.title.update({"name":"mysql"},{$set:{"name":"redis"}},{multi:true}) WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 }) > db.title.find() { "_id" : ObjectId("5eeec83e9a6da52bb561a3a5"), "name" : "mongoDB" } { "_id" : ObjectId("5eeeca769a6da52bb561a3a6"), "name" : "redis" } { "_id" : ObjectId("5eeecb6e9a6da52bb561a3a7"), "name" : "redis" } { "_id" : ObjectId("5eeecb719a6da52bb561a3a8"), "name" : "redis" }
刪除文檔
db.collection.remove(query,justOne)
:默認刪除匹配條件的所有記錄,僅要刪除一條,設置選項justOne:true或1
例如:刪除name=redis的所有記錄
> db.title.remove({"name":"redis"}) WriteResult({ "nRemoved" : 3 }) > db.title.find() { "_id" : ObjectId("5eeec83e9a6da52bb561a3a5"), "name" : "mongoDB" }
例如:僅刪除符合條件的一條記錄
> db.title.find() { "_id" : ObjectId("5eeec83e9a6da52bb561a3a5"), "name" : "mongoDB" } { "_id" : ObjectId("5eeecd339a6da52bb561a3a9"), "name" : "mysql" } { "_id" : ObjectId("5eeecd349a6da52bb561a3aa"), "name" : "mysql" } { "_id" : ObjectId("5eeecd359a6da52bb561a3ab"), "name" : "mysql" } > db.title.remove({"name":"mysql"},true) WriteResult({ "nRemoved" : 1 }) > db.title.find() { "_id" : ObjectId("5eeec83e9a6da52bb561a3a5"), "name" : "mongoDB" } { "_id" : ObjectId("5eeecd349a6da52bb561a3aa"), "name" : "mysql" } { "_id" : ObjectId("5eeecd359a6da52bb561a3ab"), "name" : "mysql" }
db.collection.remove({})
:刪除所有記錄,類似常規 SQL 的 truncate 命令
> db.title.remove({}) WriteResult({ "nRemoved" : 3 }) > db.title.find()
查詢文檔
db.collection.find()
> db.title.find() { "_id" : ObjectId("5eeec83e9a6da52bb561a3a5"), "name" : "mongoDB" } { "_id" : ObjectId("5eeecd349a6da52bb561a3aa"), "name" : "mysql" } { "_id" : ObjectId("5eeecd359a6da52bb561a3ab"), "name" : "mysql" }
db.collection.find().pretty()
:以人們較易閱讀的方式顯示查詢結果
> db.title.find().pretty() { "_id" : ObjectId("5eeed9269a6da52bb561a3ac"), "name" : "mysql" } { "_id" : ObjectId("5eeed9289a6da52bb561a3ad"), "name" : "mysql" } { "_id" : ObjectId("5eeed9339a6da52bb561a3ae"), "name" : "redis" } { "_id" : ObjectId("5eeeda6a9a6da52bb561a3af"), "name" : "redis" } { "_id" : ObjectId("5eeeda959a6da52bb561a3b0"), "name" : "redis", "country" : "china" }
MongoDB 與 RDBMS Where 語句比較
如果你熟悉常規的 SQL 數據,通過下表可以更好的理解 MongoDB 的條件語句查詢:
操作 | 格式 | 範例 | RDBMS中的類似語句 |
---|---|---|---|
等於 | {key:value} | db.col.find({"by":"mongoDB"}).pretty() | where by = 'mongoDB' |
小於 | {key:{$lt:value}} | db.col.find({"likes":{$lt:50}}).pretty() | where likes < 50 |
小於或等於 | {key:{$lte:value}} | db.col.find({"likes":{$lte:50}}).pretty() | where likes <= 50 |
大於 | {key:{$gt:value}} | db.col.find({"likes":{$gt:50}}).pretty() | where likes > 50 |
大於或等於 | {key:{$gte:value}} | db.col.find({"likes":{$gte:50}}).pretty() | where likes >= 50 |
不等於 | {key:{$ne:value}} | db.col.find({"likes":{$ne:50}}).pretty() | where likes != 50 |
and | {key1:value1, key2:value2} | db.col.find({"name":"mongoDB", "age":17}).pretty() | where name="mongoDB" and age=17 |
or | {$or:[{"key1":"value1"},{"key2": "value2"}]} | db.col.find({$or:[{"name":"mongDB教程"},{"title": "MongoDB"}]}).pretty() | where name="mongDB教程" or title="MongoDB" |
Limit與Skip方法
db.collection.find().limit(number)
例如:查詢title集合中的前兩條記錄
> db.title.find().limit(2) { "_id" : ObjectId("5eeed9269a6da52bb561a3ac"), "name" : "mysql" } { "_id" : ObjectId("5eeed9289a6da52bb561a3ad"), "name" : "mysql" }
db.collection.find().limit(number).skip(number)
例如:跳過title集合中的第一條記錄,查出兩條記錄
> db.title.find().limit(2).skip(1) { "_id" : ObjectId("5eeed9289a6da52bb561a3ad"), "name" : "mysql" } { "_id" : ObjectId("5eeed9339a6da52bb561a3ae"), "name" : "redis" }
排序
在 MongoDB 中使用 sort() 方法對數據進行排序,sort() 方法可以通過參數指定排序的字段,並使用 1 和 -1 來指定排序的方式,其中 1 爲升序排列,而 -1 是用於降序排列。
db.COllection.find().sort({key:1})
> db.title.find().sort({"likes":1}) { "_id" : ObjectId("5eef0c5d9a6da52bb561a3b3"), "name" : "mongoDB", "likes" : 10 } { "_id" : ObjectId("5eef0c399a6da52bb561a3b1"), "name" : "mysql", "likes" : 12 } { "_id" : ObjectId("5eef0c489a6da52bb561a3b2"), "name" : "redis", "likes" : 32 }
創建索引
db.collection.createIndex(keys, options)
語法中 Key 值爲你要創建的索引字段,1 爲指定按升序創建索引,如果你想按降序來創建索引指定爲 -1 即可。
> db.title.createIndex({"likes":-1})
聚合函數
db.collection.aggregate()
:類似於sql語句中的count(*)