Mongo 查询操作-官方文档

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 } } )

posted @ 2020-09-11 14:09  它山之玉  阅读(1196)  评论(0编辑  收藏  举报