命令概覽
新增
db.collection.insertOne()
New in version 3.2db.collection.insertMany()
New in version 3.2db.collection.insert()
insert()方法在主要驅(qū)動(dòng)程序中已被棄用洲押,因此武花,每當(dāng)要將單個(gè)文檔插入到集合中時(shí),都應(yīng)使用.insertOne()方法杈帐;當(dāng)要將多個(gè)文檔插入到集合中時(shí)体箕,應(yīng)使用.insertMany。同樣的情況適用于updateOne
挑童,updateMany
累铅,deleteOne
,deleteMany
站叼,findOneAndDelete
娃兽,findOneAndUpdate
和findOneAndReplace
。請(qǐng)參閱 寫(xiě)入操作概述 大年。
文檔中提到的db.collection.insert()
將一個(gè)或多個(gè)文檔插入到集合中换薄,并為單個(gè)插入返回一個(gè) WriteResult 對(duì)象,并返回一個(gè) BulkWriteResult 對(duì)象翔试,用于批量插入轻要。
注意寫(xiě)關(guān)注
db.collection.insertOne(
<document>,
{
writeConcern: <document>
}
)
--- 例子
db.products.insertOne(
{ "item": "envelopes", "qty": 100, type: "Self-Sealing" },
{ writeConcern: { w : "majority", wtimeout : 100 } }
);
查詢
- in查詢
db.inventory.find( { status: { $in: [ "A", "D" ] } } )
SELECT * FROM inventory WHERE status in ("A", "D")
盡管可以使用
$or
運(yùn)算符表示此查詢,但是 在同一字段上執(zhí)行相等性檢查時(shí)垦缅,請(qǐng)使用$in
運(yùn)算符而不是$or
運(yùn)算符冲泥。
- And和比較運(yùn)算
db.inventory.find( { status: "A", qty: { $lt: 30 } } )
SELECT * FROM inventory WHERE status = "A" AND qty < 30
有關(guān)不同BSON類型值的比較,請(qǐng)參見(jiàn)指定的BSON比較順序壁涎。
名稱 | 描述 |
---|---|
$eq |
匹配等于指定值的值凡恍。 |
$gt |
匹配大于指定值的值。 |
$gte |
匹配大于或等于指定值的值怔球。 |
$in |
匹配數(shù)組中指定的任何值嚼酝。 |
$lt |
匹配小于指定值的值。 |
$lte |
匹配小于或等于指定值的值竟坛。 |
$ne |
匹配所有不等于指定值的值闽巩。 |
$nin |
不匹配數(shù)組中指定的任何值。 |
- or條件
db.inventory.find( { $or: [ { status: "A" }, { qty: { $lt: 30 } } ] } )
SELECT * FROM inventory WHERE status = "A" OR qty < 30
- 數(shù)組查找
嚴(yán)格要求數(shù)組順序和元素担汤,有且僅有"red" 和"blank"
db.inventory.find( { tags: ["red", "blank"] } )
忽略順序和其他元素
db.inventory.find( { tags: { $all: ["red", "blank"] } } )
包含一個(gè)元素
db.inventory.find( { tags: "red" } )
多條件匹配
相當(dāng)于or
db.inventory.find( { dim_cm: { $gt: 15, $lt: 20 } } )
相當(dāng)于and,使用$elemMatch
運(yùn)算符在數(shù)組的元素上指定多個(gè)條件
db.inventory.find( { dim_cm: { $elemMatch: { $gt: 22, $lt: 30 } } } )
查詢一個(gè)元素的數(shù)組索引位置
下示例查詢dim_cm數(shù)組中第二個(gè)元素大于25的所有文檔:
db.inventory.find( { "dim_cm.1": { $gt: 25 } } )
查詢由數(shù)組的長(zhǎng)度數(shù)組,例如涎跨,以下選擇數(shù)組tags具有3個(gè)元素的文檔 。
db.inventory.find( { "tags": { $size: 3 } } )
查詢嵌套在數(shù)組中的文檔崭歧。需要完全匹配
-- 案例數(shù)據(jù)
db.inventory.insertMany( [
{ item: "journal", instock: [ { warehouse: "A", qty: 5 }, { warehouse: "C", qty: 15 } ] },
{ item: "notebook", instock: [ { warehouse: "C", qty: 5 } ] },
{ item: "paper", instock: [ { warehouse: "A", qty: 60 }, { warehouse: "B", qty: 15 } ] },
{ item: "planner", instock: [ { warehouse: "A", qty: 40 }, { warehouse: "B", qty: 5 } ] },
{ item: "postcard", instock: [ { warehouse: "B", qty: 15 }, { warehouse: "C", qty: 35 } ] }
]);
db.inventory.find( { "instock": { warehouse: "A", qty: 5 } } )
使用數(shù)組索引來(lái)查詢一個(gè)字段中嵌入文檔
db.inventory.find( { 'instock.0.qty': { $lte: 20 } } )
單個(gè)嵌套文檔在嵌套字段上滿足多個(gè)查詢條件
--下面的示例查詢文檔隅很,其中instock數(shù)組具有至少一個(gè)嵌入式文檔,該文檔同時(shí)包含 qty等于5的字段和warehouse等于的字段A:
b.inventory.find( { "instock": { $elemMatch: { qty: 5, warehouse: "A" } } } )
-- 下面的示例查詢文檔率碾,其中instock數(shù)組具有至少一個(gè)嵌入式文檔叔营,該嵌入式文檔包含qty大于10和小于或等于的字段20:
db.inventory.find( { "instock": { $elemMatch: { qty: { $gt: 10, $lte: 20 } } } } )
- 返回特定字段內(nèi)嵌文件
The following example returns:
1屋彪、The _id field (returned by default),
2、The item field,
3绒尊、The status field,
4撼班、The uom field in the size document.
The uom field remains embedded in the size document.
-- 案例數(shù)據(jù)
db.inventory.insertMany( [
{ item: "journal", status: "A", size: { h: 14, w: 21, uom: "cm" }, instock: [ { warehouse: "A", qty: 5 } ] },
{ item: "notebook", status: "A", size: { h: 8.5, w: 11, uom: "in" }, instock: [ { warehouse: "C", qty: 5 } ] },
{ item: "paper", status: "D", size: { h: 8.5, w: 11, uom: "in" }, instock: [ { warehouse: "A", qty: 60 } ] },
{ item: "planner", status: "D", size: { h: 22.85, w: 30, uom: "cm" }, instock: [ { warehouse: "A", qty: 40 } ] },
{ item: "postcard", status: "A", size: { h: 10, w: 15.25, uom: "cm" }, instock: [ { warehouse: "B", qty: 15 }, { warehouse: "C", qty: 35 } ] }
]);
內(nèi)嵌文件中抑制特定的字段
-- 排除size中uom返回
db.inventory.find(
{ status: "A" },
{ "size.uom": 0 }
)
數(shù)組中字段返回
-- 返回instock數(shù)組中的qty字段
db.inventory.find( { status: "A" }, { item: 1, status: 1, "instock.qty": 1 } )
回?cái)?shù)組中的項(xiàng)目特定數(shù)組元素
以下示例使用$slice
投影運(yùn)算符返回instock
數(shù)組中的最后一個(gè)元素:
db.inventory.find( { status: "A" }, { item: 1, status: 1, instock: { $slice: -1 } } )
- null查詢
db.inventory.insertMany([
{ _id: 1, item: null },
{ _id: 2 }
])
-- 以上兩個(gè)語(yǔ)句都匹配
> db.inventory.find( { item: null } )
{ "_id" : 1, "item" : null }
{ "_id" : 2 }
-- The query returns only the document where the item field has a value of null.
> db.inventory.find( { item : { $type: 10 } } )
{ "_id" : 1, "item" : null }
-- 檢查字段是否存在
> db.inventory.find( { item : { $exists: false } } )
{ "_id" : 2 }
修改
-
db.collection.updateOne()
New in version 3.2 -
db.collection.updateMany()
New in version 3.2 -
db.collection.replaceOne()
New in version 3.2
- 更新單個(gè)文檔
更新第一個(gè)item='paper'的文檔
- 使用
$set
操作員更新的值size.uom
字段"cm"
和值status
字段"P"
, - 使用
$currentDate
運(yùn)算符將lastModified
字段的值更新為當(dāng)前日期垒酬。如果lastModified
字段不存在,$currentDate
將創(chuàng)建該字段件炉。有關(guān)$currentDate
詳細(xì)信息勘究,請(qǐng)參見(jiàn) 。
client[:inventory].update_one({ item: 'paper'},
{ '$set' => { 'size.uom' => 'cm', 'status' => 'P' },
'$currentDate' => { 'lastModified' => true } })
- 更新多個(gè)文檔
更新qty小于50的所有文檔
client[:inventory].update_many({ qty: { '$lt' => 50 } },
{ '$set' => { 'size.uom' => 'in', 'status' => 'P' },
'$currentDate' => { 'lastModified' => true } })
- 替換文檔
要替換除_id
字段以外的文檔的所有內(nèi)容斟冕,請(qǐng)將一個(gè)全新的文檔作為第二個(gè)參數(shù)傳遞給 replace_one()
替換文檔可以具有與原始文檔不同的字段口糕。在替換文檔中,_id由于該_id字段是不可變的磕蛇,因此可以省略該字段景描;但是,如果您包含該_id字段秀撇,則該 字段必須與當(dāng)前值具有相同的值超棺。
client[:inventory].replace_one({ item: 'paper' },
{ item: 'paper',
instock: [ { warehouse: 'A', qty: 60 },
{ warehouse: 'B', qty: 40 } ] })
刪除
-
db.collection.deleteOne()
New in version 3.2 -
db.collection.deleteMany()
New in version 3.2
- 批量寫(xiě)入和重試
分為有序和無(wú)序批量寫(xiě)入,使用第二個(gè)參數(shù){ ordered : false } 控制呵燕,默認(rèn)有序
try {
db.characters.bulkWrite(
[
{ insertOne :
{
"document" :
{
"_id" : 4, "char" : "Dithras", "class" : "barbarian", "lvl" : 4
}
}
},
{ insertOne :
{
"document" :
{
"_id" : 5, "char" : "Taeln", "class" : "fighter", "lvl" : 3
}
}
},
{ updateOne :
{
"filter" : { "char" : "Eldon" },
"update" : { $set : { "status" : "Critical Injury" } }
}
},
{ deleteOne :
{ "filter" : { "char" : "Brisbane"} }
},
{ replaceOne :
{
"filter" : { "char" : "Meldane" },
"replacement" : { "char" : "Tanys", "class" : "oracle", "lvl" : 4 }
}
}
]
);
}
catch (e) {
print(e);
}