MongoDB 官方文檔:https://docs.mongodb.com/manual/tutorial/query-documents/
官方文檔啥都有,本篇只列舉幾個簡單的例子。
本篇基本都是機器翻譯+部分手動修改,建議直接看官方文檔減少歧義。
Mongo Query
溫馨提示:
有的客戶端查不出來數據,可能是因為數字類型沒有使用
NumberLong
這種聲明類型的包起來
簡單查詢不做說明了
// 查詢全部
db.inventory.find( {} )
// 下面的示例從庫存集合中選擇狀態等於“ d”的所有文檔:
db.inventory.find( { status: "D" } )
// 下面的示例從狀態等於“ a”或“ d”的庫存集合中檢索所有文檔:
db.inventory.find( { status: { $in: [ "A", "D" ] } } )
注意:雖然可以使用 $or 運算符表示此查詢,但在對同一字段執行相等性檢查時,使用 $in 運算符而不是 $or運算符。
// 下面的示例檢索庫存集合中狀態等於“ a”且數量小於($lt)30的所有文檔:
db.inventory.find( { status: "A", qty: { $lt: 30 } } )
// 下面的示例檢索集合中狀態等於“ a”或數量小於($lt)30的所有文檔:
db.inventory.find( { $or: [ { status: "A" }, { qty: { $lt: 30 } } ] } )
//在下面的示例中,復合查詢文檔選擇集合中狀態等於“ a”且數量小於($lt)30或項以字符 p 開頭的所有文檔:
db.inventory.find( {
status: "A",
$or: [ { qty: { $lt: 30 } }, { item: /^p/ } ]
} )
// 該操作對應於以下 SQL 語句:
// SELECT * FROM inventory WHERE status = "A" AND ( qty < 30 OR item LIKE "p%")
// 注意: MongoDB 支持正則表達式 $regex 查詢來執行字符串模式匹配。
1. 嵌入式/嵌套式文檔查詢
本頁面提供了在 mongo shell 中使用 db.collection.find ()方法進行查詢操作的示例。本頁上的示例使用庫存集合。要填充庫存集合,請運行以下命令:
db.inventory.insertMany([
{ item: "journal", qty: 25, size: { h: 14, w: 21, uom: "cm" }, status: "A" },
{ item: "notebook", qty: 50, size: { h: 8.5, w: 11, uom: "in" }, status: "A" },
{ item: "paper", qty: 100, size: { h: 8.5, w: 11, uom: "in" }, status: "D" },
{ item: "planner", qty: 75, size: { h: 22.85, w: 30, uom: "cm" }, status: "D" },
{ item: "postcard", qty: 45, size: { h: 10, w: 15.25, uom: "cm" }, status: "A" }
]);
1.1 Match an Embedded/Nested Document
例如,下面的查詢選擇字段size等於文檔{h:14,w:21,uom:“cm”}
的所有文檔:
db.inventory.find( { size: { h: 14, w: 21, uom: "cm" } } )
整個嵌入文檔的相等匹配要求與指定的 < 值 > 文檔完全匹配,包括字段順序。例如,下面的查詢與庫存集合中的任何文檔都不匹配:
db.inventory.find( { size: { w: 21, h: 14, uom: "cm" } } )
1.2 Query on Nested Field
若要對嵌入/嵌套文檔中的字段指定查詢條件,請使用點符號(“ field.nestedField”)
。
When querying using dot notation, the field and nested field must be inside quotation marks.
在使用點符號查詢時,字段和嵌套字段必須位於引號內。
db.inventory.find( { "size.uom": "in" } )
db.inventory.find( { "size.h": { $lt: 15 } } )
// 下面的查詢選擇所有的文檔,其中嵌套字段 h 小於15,嵌套字段 uom 等於“ in” ,狀態字段等於“ d” :
db.inventory.find( { "size.h": { $lt: 15 }, "size.uom": "in", status: "D" } )
2. Query an Array
查詢數組
本頁面提供了使用 mongo shell 中的 db.collection.find ()方法對數組字段進行查詢操作的示例。本頁上的示例使用庫存集合。要填充庫存集合,請運行以下命令:
db.inventory.insertMany([
{ item: "journal", qty: 25, tags: ["blank", "red"], dim_cm: [ 14, 21 ] },
{ item: "notebook", qty: 50, tags: ["red", "blank"], dim_cm: [ 14, 21 ] },
{ item: "paper", qty: 100, tags: ["red", "blank", "plain"], dim_cm: [ 14, 21 ] },
{ item: "planner", qty: 75, tags: ["blank", "red"], dim_cm: [ 22.85, 30 ] },
{ item: "postcard", qty: 45, tags: ["blue"], dim_cm: [ 10, 15.25 ] }
]);
2.1 匹配數組
要在數組中指定相等條件,請使用查詢文檔{ < field > : < value > } ,其中 < value > 是要匹配的精確數組,包括元素的順序。
//下面的示例查詢所有文檔,其中tags是一個數組,其中正好有兩個元素,按指定順序為“ red”和“ blank” :
db.inventory.find( { tags: ["red", "blank"] } )
// 相反,如果你想找到一個同時包含“ red”和“ blank”元素的數組,不管數組中的順序或其他元素,使用 $all 運算符:
db.inventory.find( { tags: { $all: ["red", "blank"] } } )
2.2 為元素查詢數組
// 下面的示例查詢所有文檔,其中tags是一個數組,包含字符串“ red”作為其元素之一:
db.inventory.find( { tags: "red" } )
// 例如,下面的操作查詢所有數組 dim_cm 至少包含一個值大於25的元素的文檔。
db.inventory.find( { dim_cm: { $gt: 25 } } )
2.3 為數組元素指定多個條件
數組元素上具有復合過濾條件的數組查詢
// 下面的例子查詢文檔,其中 dim _ cm 數組包含的元素在某些組合中滿足查詢條件; 例如,一個元素可以滿足大於15的條件,另一個元素可以滿足小於20的條件,或者單個元素可以同時滿足這兩個條件:
db.inventory.find( { dim_cm: { $gt: 15, $lt: 20 } } )
滿足多個條件的數組元素的查詢
使用 $elemMatch 運算符對數組元素指定多個條件,以便至少有一個數組元素滿足所有指定的條件。
// 下面的示例查詢文檔,其 dim _ cm 數組至少包含一個既大於($gt)22又小於($lt)30的元素:
db.inventory.find( { dim_cm: { $elemMatch: { $gt: 22, $lt: 30 } } } )
按數組索引位置查詢元素
使用點表示法,可以為數組的特定索引或位置指定元素的查詢條件。數組使用從零開始的索引。
注意:When querying using dot notation, the field and nested field must be inside quotation marks.
使用點表示法時,字段必須位於括號內
// 下面的示例查詢數組 dim _ cm 中第二個元素大於25的所有文檔
db.inventory.find( { "dim_cm.1": { $gt: 25 } } )
按數組長度查詢數組
// 使用 $size 運算符按元素數查詢數組。例如,下面選擇數組tags有3個元素的文檔
db.inventory.find( { "tags": { $size: 3 } } )
3 查詢嵌入式文檔數組
本頁面提供了使用 mongo shell 中的 db.collection.find ()方法對嵌套文檔數組進行查詢操作的示例。本頁上的示例使用庫存集合。要填充庫存集合,請運行以下命令
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 } ] }
]);
3.1 對嵌套在數組中的文檔的查詢
// 下面的示例選擇 instock 數組中的元素與指定文檔匹配的所有文檔
db.inventory.find( { "instock": { warehouse: "A", qty: 5 } } )
// 整個嵌入/嵌套文檔上的等式匹配需要與指定文檔完全匹配,包括字段順序。例如,下面的查詢與庫存集合中的任何文檔都不匹配
db.inventory.find( { "instock": { qty: 5, warehouse: "A" } } )
3.2 在文檔數組的字段上指定查詢條件
在嵌入到文檔數組中的字段上指定查詢條件
如果不知道嵌套在數組中的文檔的索引位置,請將數組字段的名稱與一個點(.)連接起來以及嵌套文檔中字段的名稱
// 所有instock數組中,至少有一個嵌入文檔包含 qty值小於或等於20
db.inventory.find( { 'instock.qty': { $lte: 20 } } )
使用數組索引查詢嵌入文檔中的字段
使用點表示法,您可以在文檔中數組的特定索引或位置為字段指定查詢條件。數組使用從零開始的索引
(使用點表示法時,字段必須位於括號內)
// 下面的示例選擇所有的 instock 數組的第一個元素是包含字段qty的文檔,其值小於等於20
db.inventory.find( { 'instock.0.qty': { $lte: 20 } } )
3.3 為文檔數組指定多個條件
當對嵌套在文檔數組中的多個字段指定條件時,可以指定查詢,使單個文檔滿足這些條件,或者數組中的任何文檔組合(包括單個文檔)滿足這些條件。
單個嵌套文檔在嵌套字段上滿足多個查詢條件
使用 $elemMatch 運算符在嵌入文檔數組上指定多個條件,這樣至少有一個嵌入文檔滿足所有指定的條件
// 下面的示例查詢文檔,其中 instock 數組至少有一個嵌入文檔,該文檔包含字段 qty 等於5,字段 warehouse 等於 A:
db.inventory.find( { "instock": { $elemMatch: { qty: 5, warehouse: "A" } } } )
// 下面的示例查詢 instock 數組中至少有一個嵌入文檔包含大於10且小於或等於20的字段 qty 的文檔:
db.inventory.find( { "instock": { $elemMatch: { qty: { $gt: 10, $lte: 20 } } } } )
滿足條件的元素組合
如果數組字段上的復合查詢條件不使用 $elemMatch 運算符,則查詢將選擇其數組包含滿足條件的任何元素組合的文檔。
// 例如,如果 instock 數組中嵌套的任何文檔的 qty 字段大於10,而且該數組中的任何文檔(但不一定是同一嵌入文檔)的 qty 字段小於或等於20,那么下面的查詢將匹配這些文檔:
db.inventory.find( { "instock.qty": { $gt: 10, $lte: 20 } } )
// 下面的示例查詢文檔,其中 instock 數組至少有一個包含字段 qty 等於5的嵌入式文檔,以及至少一個包含字段 warehouse 等於 a 的嵌入式文檔(但不一定是相同的嵌入式文檔) :
db.inventory.find( { "instock.qty": 5, "instock.warehouse": "A" } )
4 從查詢返回的項目字段
默認情況下,MongoDB 中的查詢返回匹配文檔中的所有字段。為了限制 MongoDB 發送給應用程序的數據量,您可以包含一個投影文檔來指定或限制返回的字段。
本頁面提供了使用 mongo shell 中的 db.collection.find ()方法進行投影查詢操作的示例。本頁上的示例使用庫存集合。要填充庫存集合,請運行以下命令:
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 } ] }
]);
4.1 返回匹配文檔中的所有字段
如果沒有指定 projection 文檔,db.collection.find ()
方法將返回匹配文檔中的所有字段。
// 下面的示例返回清單集合中狀態等於“ a”的所有文檔的所有字段:
db.inventory.find( { status: "A" } )
// 該操作對應於以下 SQL 語句:
SELECT * from inventory WHERE status = "A"
4.2 只返回指定字段和 _id 字段
// 通過在投影文檔中將 < field > 設置為1,一個投影可以顯式地包含幾個字段。下面的操作返回與查詢匹配的所有文檔。在結果集中,匹配文檔中只返回項、狀態和 _ id 字段。
db.inventory.find( { status: "A" }, { item: 1, status: 1 } )
// 該操作對應於以下 SQL 語句:
SELECT _id, item, status from inventory WHERE status = "A"
4.3 隱藏_id 字段
// 可以通過在投影中將 _ id 字段設置為0來從結果中刪除該字段,如下例所示:
db.inventory.find( { status: "A" }, { item: 1, status: 1, _id: 0 } )
// 該操作對應於以下 SQL 語句:
SELECT item, status from inventory WHERE status = "A"
注意: 除了 _id 字段之外,您不能在投影文檔中組合包含和排除語句。
With the exception of the
_id
field, you cannot combine inclusion and exclusion statements in projection documents.
4.4 返回除了被排除的字段以外的所有字段
// 與列出匹配文檔中要返回的字段不同,您可以使用投影來排除特定的字段。下面的示例返回除了匹配文檔中的 status 和 instock 字段之外的所有字段
db.inventory.find( { status: "A" }, { status: 0, instock: 0 } )
注意: 除了 _id 字段之外,您不能在投影文檔中組合包含和排除語句。
With the exception of the
_id
field, you cannot combine inclusion and exclusion statements in projection documents.
4.5 返回嵌入文檔中的特定字段
可以在嵌入的文檔中返回特定的字段。在投影文檔中,使用點表示法引用嵌入字段並設置為1。
下面的字段返回:
_id
(默認返回),item
,status
,uom
字段(在size
文檔中).
uom 字段仍然嵌入在 size 文檔中。
db.inventory.find(
{ status: "A" },
{ item: 1, status: 1, "size.uom": 1 }
)
從 MongoDB 4.4開始,您還可以使用嵌套形式指定嵌入字段,例如{ item: 1,status: 1,size: { uom: 1}。
4.6 隱藏嵌入文檔中的特定字段
可以在嵌入的文檔中禁用特定字段。使用點表示法引用投影文檔中的嵌入字段並將其設置為0。
下面的示例指定排除 size 文檔中的 uom 字段的投影。所有其他字段都在匹配文檔中返回:
db.inventory.find(
{ status: "A" },
{ "size.uom": 0 }
)
從 MongoDB 4.4開始,您還可以使用嵌套形式指定嵌入字段,例如{ size: { uom: 0}}。
4.7 數組中嵌入文檔的投影
使用點符號將特定字段投影到嵌入在數組中的文檔中。
下面的示例指定要返回的投影:
_id
(默認返回),item
,status
,qty
(嵌入在instock
數組中)
db.inventory.find( { status: "A" }, { item: 1, status: 1, "instock.qty": 1 } )
4.8 返回數組中的項目特定數組元素
對於包含數組的字段,MongoDB 提供了以下操作數組的投影操作符: $elemMatch
, $slice
, 和$
.
下面的示例使用 $slice 投影操作符返回 instock 數組中的最后一個元素:
db.inventory.find( { status: "A" }, { item: 1, status: 1, instock: { $slice: -1 } } )
$elemMatch、 $slice 和 $是將特定元素投射到返回數組中的唯一方法。例如,您不能使用數組索引投影特定的數組元素; 例如{“ instock. 0” : 1}投影不會將數組投影到第一個元素。
($elemMatch
, $slice
, and $
are the only way to project specific elements to include in the returned array. For instance, you cannot project specific array elements using the array index; e.g. { "instock.0": 1 }
projection will not project the array with the first element.)
5. 查詢null字段或缺失字段
本頁上的示例使用庫存集合。要填充庫存集合,請運行以下命令:
db.inventory.insertMany([
{ _id: 1, item: null },
{ _id: 2 }
])
5.1 Equality Filter 相等篩選器
{ item: null }查詢匹配包含其值為 null 的 item 字段或不包含 item 字段的文檔
db.inventory.find( { item: null } )
查詢返回集合中的兩個文檔。
5.2 Type Check 類型檢查
{ item : { $type: 10 } }
查詢只匹配包含其值為 Null 的 item 字段的文檔; 也就是說,item 字段的值為 BSON Type Null (類型10) :
db.inventory.find( { item : { $type: 10 } } )
查詢只返回項字段值為 null 的文檔。
5.3 Existence Check 存在檢查
{ item: { $exists: false }
查詢與不包含 item 字段的文檔匹配:
db.inventory.find( { item : { $exists: false } } )