具體詳細函數請參閱MongoDB文檔
數據庫
- 創建以及切換數據庫(第一次創建數據庫並不真正的創建數據庫, 只有插入第一條數據之後數據庫纔會真正創建)
use DATABASE_NAME
- 查看當前數據庫
db
- 查看所有數據庫
show dbs
- 刪除數據庫(要切換到對應的數據庫下)
db.dropDatabase()
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
test 0.000GB
>
> use testoob
switched to db testoob
>
> db
testoob
>
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
test 0.000GB
>
>
> db.testoob.insertOne({})
{
"acknowledged" : true,
"insertedId" : ObjectId("5c1b52afb00d4fb0d9ef58a7")
}
>
>
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
test 0.000GB
testoob 0.000GB
>
> db
testoob
>
> db.dropDatabase()
{ "dropped" : "testoob", "ok" : 1 }
>
集合
- 創建集合
1. db.createCollection(name, options)
2. db.COLLECTION_NAME.insert({}) --> 當你插入一些文檔時,MongoDB 會自動創建集合。
字段 | 類型 | 描述 |
---|---|---|
capped | 布爾 | (可選)如果爲 true,則創建固定集合。固定集合是指有着固定大小的集合,當達到最大值時,它會自動覆蓋最早的文檔。 當該值爲 true 時,必須指定 size 參數。 |
autoIndexId | 布爾 | (可選)如爲 true,自動在 _id 字段創建索引。默認爲 false。 |
size | 數值 | (可選)爲固定集合指定一個最大值(以字節計)。 如果 capped 爲 true,也需要指定該字段。 |
max | 數值 | (可選)指定固定集合中包含文檔的最大數量。 |
- 查看已有集合
show collections / show tales
- 刪除集合
db.COLLECTION_NAME.drop()
>
> use test
switched to db test
>
> show tables
>
> show collections
>
> db.createCollection("user01")
{ "ok" : 1 }
>
> show tables
user01
>
> db.createCollection("user02",{capped:true, autoIndexId: true, size: 6142800, max:10000})
{
"note" : "the autoIndexId option is deprecated and will be removed in a future release",
"ok" : 1
}
>
> show tables
user01
user02
>
> db.user01.drop()
true
>
> db.user02.drop()
true
>
文檔
- 插入文檔(insert)
db.COLLECTION_NAME.insertOne(
<document>,
{
writeConcern: <document>
}
)
writeConcern :可選, 事務相關寫入關注, 如果使用事務 不要使用這個字段, 請參閱 事務選項(讀取關注/寫入關注/讀取首選項)
db.COLLECTION_NAME.insert(
<document or array of documents>,
{
writeConcern: <document>,
ordered: <boolean>
}
)
ordered :
可選的。如果true,在數組中執行文檔的有序插入,並且如果其中一個文檔發生錯誤,MongoDB將返回而不處理數組中的其餘文檔。
如果false執行無序插入,並且如果其中一個文檔發生錯誤,則繼續處理陣列中的其餘文檔。
- 插入文檔(save)
db.collection.save(
<document>,
{
writeConcern: <document>
}
)
writeConcern :可選, 事務相關寫入關注, 如果使用事務 不要使用這個字段, 請參閱 事務選項(讀取關注/寫入關注/讀取首選項)
區別: save() 方法如果不指定 _id 字段類似於 insert() 方法。如果指定 _id 字段,則會更新 匹配 該 _id 的數據。
>
> db.users.insert({name:"user001", age:18})
WriteResult({ "nInserted" : 1 })
>
>
> db.users.find()
{ "_id" : ObjectId("5c1b610eb00d4fb0d9ef58ab"), "name" : "user001", "age" : 18 }
>
> db.users.save({"_id": 123114, "name":"user001", age:18})
WriteResult({ "nMatched" : 0, "nUpserted" : 1, "nModified" : 0, "_id" : 123114 })
>
> db.users.find()
{ "_id" : ObjectId("5c1b610eb00d4fb0d9ef58ab"), "name" : "user001", "age" : 18 }
{ "_id" : 123114, "name" : "user001", "age" : 18 }
>
> db.users.save({"_id": 123114, "name":"user003", age:18})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
>
> db.users.find()
{ "_id" : ObjectId("5c1b610eb00d4fb0d9ef58ab"), "name" : "user001", "age" : 18 }
{ "_id" : 123114, "name" : "user003", "age" : 18 }
>
>
> db.users.insert({"name":"user004", "name":"user005"})
WriteResult({ "nInserted" : 1 })
>
> db.users.find()
{ "_id" : ObjectId("5c1b610eb00d4fb0d9ef58ab"), "name" : "user001", "age" : 18 }
{ "_id" : 123114, "name" : "user003", "age" : 18 }
{ "_id" : ObjectId("5c1b61d7b00d4fb0d9ef58ac"), "name" : "user005" }
>
>
> db.users.insert({"name":"user006","NAME":"user007"})
WriteResult({ "nInserted" : 1 })
>
> db.users.find()
{ "_id" : ObjectId("5c1b610eb00d4fb0d9ef58ab"), "name" : "user001", "age" : 18 }
{ "_id" : 123114, "name" : "user003", "age" : 18 }
{ "_id" : ObjectId("5c1b61d7b00d4fb0d9ef58ac"), "name" : "user005" }
{ "_id" : ObjectId("5c1b626db00d4fb0d9ef58ad"), "name" : "user006", "NAME" : "user007" }
>
注意:
- 同一文檔中不能存在相同的 key, 存在相同的key後面的數據會把前面的數據覆蓋掉
- 文檔中鍵值區分大小寫
- 查詢
db.collection.findOne(query, projection).pretty()
db.collection.find(query, projection).pretty()
query : 可選, 指定的查詢條件
projection : 可選, 使用投影操作符返回制定的鍵, 當查詢全部鍵值可以忽略 1: 返回 0: 忽略
pretty() : 以格式化的方式顯示所有文檔
db.users.findOne({name:"user001"},{name:1}) √
db.users.findOne({name:"user001"},{_id : 0}) √
db.users.find({name:"user001"},{name:1, age: 0}) × projection不能混合使用
查詢條件
描述 | 格式 | 範例 | RDBMS中的類似語句 |
---|---|---|---|
等於 | {<key>:<value> } |
db.users.find({"name":"user001"}).pretty() |
where name = 'user001' |
小於 | {<key>:{$lt:<value>}} |
db.col.find({"age":{$lt:20}}).pretty() |
where age < 50 |
小於或等於 | {<key>:{$lte:<value>}} |
db.users.find({"age":{$lte:20}}).pretty() |
where age <= 50 |
大於 | {<key>:{$gt:<value>}} |
db.users.find({"age":{$gt:50}}).pretty() |
where age > 20 |
大於或等於 | {<key>:{$gte:<value>}} |
db.users.find({"age":{$gte:50}}).pretty() |
where age >= 20 |
不等於 | {<key>:{$ne:<value>}} |
db.users.find({"age":{$ne:50}}).pretty() |
where age != 20 |
AND 條件
MongoDB 的 find() 方法可以傳入多個鍵(key),每個鍵(key)以逗號隔開,即常規 SQL 的 AND 條件。類似於 RDBMS 中的
WHERE key1 = value1 AND key2 = value2
語法格式如下:
db.COLLECTION_NAME.find({key1:value1, key2:value2}).pretty()
OR 條件
MongoDB OR 條件語句使用了關鍵字 $or,類似於RDBMS 中的 WHEHRE key1 = value1 OR key2 = value2
語法格式如下:
db.COLLECTION_NAME.find({$or[{kay1:value1},{key2:value2}]}).pretty()
- 刪除
刪除一個
db.collection.deleteOne(
<filter>,
{
writeConcern: <document>,
collation: <document>
}
)
writeConcern :可選, 事務相關寫入關注, 如果使用事務 不要使用這個字段, 請參閱 事務選項(讀取關注/寫入關注/讀取首選項)
collation : 可選, 要用於操作的排序規則, 詳情請參閱排序文檔。
collation: {
locale: <string>,
caseLevel: <boolean>,
caseFirst: <string>,
strength: <int>,
numericOrdering: <boolean>,
alternate: <string>,
maxVariable: <string>,
backwards: <boolean>
}
刪除多個
db.collection.deleteMany(
<filter>,
{
writeConcern: <document>,
collation: <document>
}
)
刪除一個或多個
db.collection.remove(
<query>,
<justOne>
)
justOne :<boolean> true : 只刪除第一個 false :刪除多個
db.collection.remove(
<query>,
{
justOne: <boolean>,
writeConcern: <document>,
collation: <document>
}
)
justOne :<boolean> true : 只刪除第一個 false :刪除多個
writeConcern :可選, 事務相關寫入關注, 如果使用事務 不要使用這個字段, 請參閱 事務選項(讀取關注/寫入關注/讀取首選項)
collation : 可選, 要用於操作的排序規則, 詳情請參閱排序文檔。
- 更新
db.collection.update(
<query>,
<update>,
{
upsert: <boolean>,
multi: <boolean>,
writeConcern: <document>,
collation: <document>,
arrayFilters: [ <filterdocument1>, ... ]
}
)
query: 查詢條件
udpate: 更新字段 $set, $uset, $inc, 如果替換所有都不加
upsert: 可選,如果upsert是true且沒有文檔與查詢條件匹配,則update()插入單個文檔, 如果upsert是true且存在與查詢條件匹配的文檔,則update()執行更新。如果 upsert爲true 時, 最好添加唯一索引
**multi **: 可選的,如果設置爲true,則更新符合query 條件的多個文檔。如果設置爲false,則更新一個文檔。默認值爲false。有關其他信息,請參閱多參數。
writeConcern :可選, 事務相關寫入關注, 如果使用事務 不要使用這個字段, 請參閱 事務選項(讀取關注/寫入關注/讀取首選項)
collation : 可選, 要用於操作的排序規則, 詳情請參閱排序文檔。
arrayFilters: 可選的,一組過濾器文檔,用於確定要爲陣列字段上的更新操作修改哪些數組元素。對文檔內的列表進行篩選.
##更新元素匹配arrayFilters條件
db.students.insert([
{ "_id" : 1, "grades" : [ 95, 92, 90 ] },
{ "_id" : 2, "grades" : [ 98, 100, 102 ] },
{ "_id" : 3, "grades" : [ 95, 110, 100 ] }
])
---------------------------------------------------------
db.students.update(
{ grades: { $gte: 100 } },
{ $set: { "grades.$[element]" : 100 } },
{
multi: true,
arrayFilters: [ { "element": { $gte: 100 } } ]
}
)
---------------------------------------------------------
{ "_id" : 1, "grades" : [ 95, 92, 90 ] }
{ "_id" : 2, "grades" : [ 98, 100, 100 ] }
{ "_id" : 3, "grades" : [ 95, 100, 100 ] }
##更新文檔數組的特定元素
{
"_id" : 1,
"grades" : [
{ "grade" : 80, "mean" : 75, "std" : 6 },
{ "grade" : 85, "mean" : 90, "std" : 4 },
{ "grade" : 85, "mean" : 85, "std" : 6 }
]
}
{
"_id" : 2,
"grades" : [
{ "grade" : 90, "mean" : 75, "std" : 6 },
{ "grade" : 87, "mean" : 90, "std" : 3 },
{ "grade" : 85, "mean" : 85, "std" : 4 }
]
}
-------------------------------------------------------
db.students2.update(
{ },
{ $set: { "grades.$[elem].mean" : 100 } },
{
multi: true,
arrayFilters: [ { "elem.grade": { $gte: 85 } } ]
}
)
--------------------------------------------------------
{
"_id" : 1,
"grades" : [
{ "grade" : 80, "mean" : 75, "std" : 6 },
{ "grade" : 85, "mean" : 100, "std" : 4 },
{ "grade" : 85, "mean" : 100, "std" : 6 }
]
}
{
"_id" : 2,
"grades" : [
{ "grade" : 90, "mean" : 100, "std" : 6 },
{ "grade" : 87, "mean" : 100, "std" : 3 },
{ "grade" : 85, "mean" : 100, "std" : 4 }
]
}