“文档” 菜单
文档首页
/
MongoDB Manual
/ / / /

$eq

在此页面上

  • 兼容性
  • 语法
  • 行为
  • 举例
$eq

指定相等条件。$eq 操作符匹配字段值等于指定值的文档。

可以使用 $eq 查找托管在以下环境中的部署:

  • MongoDB Atlas :用于在云中部署 MongoDB 的完全托管服务

  • MongoDB Enterprise:基于订阅、自我管理的 MongoDB 版本

  • MongoDB Community:源代码可用、免费使用且可自行管理的 MongoDB 版本

$eq 操作符采用以下形式:

{ <field>: { $eq: <value> } }

指定 $eq 运算符等同于使用 { field: <value> } 形式,但 <value> 是正则表达式的情况除外。请参阅以下示例

关于不同 BSON 类型值的比较,请参阅指定的 BSON 比较顺序。

如果指定的 <value> 是文档,则文档中字段的顺序很重要。

如果指定的 <value> 是数组,则 MongoDB 会匹配 <field> 与数组完全匹配或 <field> 包含与数组完全匹配的元素的文档。元素的顺序很重要。有关示例,请参阅等于数组值。

表达式{ field: <value> }隐式指定对<value>的匹配。MongoDB 将隐式匹配转换为更显式的形式。

<value> 固定时(如特定字符串),此表达式相当于使用 $eq 操作符 { field: { $eq: <value> } }

如果 <value> 是正则表达式,则使用 $regex 操作符 { field: { $regex: <value> } } 对语句进行扩展。

有关说明此行为的示例,请参阅 Regex 匹配行为。

始终将显式格式 { field: { $eq: <value> } } 与用户提供的输入结合使用,避免出现恶意格式的查询问题。

以下示例对包含以下文档的 inventory 集合进行查询:

db.inventory.insertMany( [
{ _id: 1, item: { name: "ab", code: "123" }, qty: 15, tags: [ "A", "B", "C" ] },
{ _id: 2, item: { name: "cd", code: "123" }, qty: 20, tags: [ "B" ] },
{ _id: 3, item: { name: "ij", code: "456" }, qty: 25, tags: [ "A", "B" ] },
{ _id: 4, item: { name: "xy", code: "456" }, qty: 30, tags: [ "B", "A" ] },
{ _id: 5, item: { name: "mn", code: "000" }, qty: 20, tags: [ [ "A", "B" ], "C" ] }
] )

以下示例将查询 inventory 集合,以选择 qty 字段的值等于 20 的所有文档:

db.inventory.find( { qty: { $eq: 20 } } )

此查询相当于:

db.inventory.find( { qty: 20 } )

这两个查询均匹配以下文档:

[
{ _id: 2, item: { name: "cd", code: "123" }, qty: 20, tags: [ "B" ] },
{ _id: 5, item: { name: "mn", code: "000" }, qty: 20, tags: [ [ "A", "B" ], "C" ] }
]

以下示例查询 inventory 集合,选择 item 文档中 name 字段的值等于 "ab" 的所有文档。要对嵌入式文档中的字段指定条件,使用点符号

db.inventory.find( { "item.name": { $eq: "ab" } } )

此查询相当于:

db.inventory.find( { "item.name": "ab" } )

这两个查询均匹配以下文档:

[ { _id: 1, item: { name: "ab", code: "123" }, qty: 15, tags: [ "A", "B", "C" ] } ]

提示

另请参阅:

以下示例查询 inventory 集合以选择 tags 数组包含值为 "B" [1] 的元素的所有文档:

db.inventory.find( { tags: { $eq: "B" } } )

此查询相当于:

db.inventory.find( { tags: "B" } )

这两个查询均匹配以下文档:

[
{ _id: 1, item: { name: "ab", code: "123" }, qty: 15, tags: [ "A", "B", "C" ] },
{ _id: 2, item: { name: "cd", code: "123" }, qty: 20, tags: [ "B" ] },
{ _id: 3, item: { name: "ij", code: "456" }, qty: 25, tags: [ "A", "B" ] },
{ _id: 4, item: { name: "xy", code: "456" }, qty: 30, tags: [ "B", "A" ] }
]

提示

另请参阅:

[1] 此查询还匹配 tags 字段的值为字符串 "B" 的文档。

以下示例将查询 inventory 集合以选择 tags 数组与指定数组完全相同或 tags 数组包含等于数组 [ "A", "B" ] 中某一元素的所有文档。

db.inventory.find( { tags: { $eq: [ "A", "B" ] } } )

此查询相当于:

db.inventory.find( { tags: [ "A", "B" ] } )

这两个查询均匹配以下文档:

[
{ _id: 3, item: { name: "ij", code: "456" }, qty: 25, tags: [ "A", "B" ] },
{ _id: 5, item: { name: "mn", code: "000" }, qty: 20, tags: [ [ "A", "B" ], "C" ] }
]

以下示例演示隐式正则表达式匹配和显式正则表达式匹配的行为差异。考虑包含以下文档的集合:

db.companies.insertMany( [
{ _id: 001, company: "MongoDB" },
{ _id: 002, company: "MongoDB2" }
] )
$eq 与字符串匹配

无论是隐式匹配还是显式使用 $eq,字符串都会扩展,以返回相同的值。这两个查询:

db.collection.find( { company: "MongoDB" }, {_id: 0 })
db.collection.find( { company: { $eq: "MongoDB" } }, {_id: 0 } )

返回以下结果:

[ { company: "MongoDB" } ]
$eq 匹配正则表达式

使用 $eq 和正则表达式的显式查询只能匹配同样是正则表达式的对象。由于 company 字段中的值是字符串,所以该示例查询不会返回任何内容。

db.companies.find( { company: { $eq: /MongoDB/ } }, {_id: 0 } )
正则表达式匹配项

对正则表达式进行隐式匹配的查询相当于使用 $regex 操作符进行查询。这两个查询:

db.companies.find( { company: /MongoDB/ }, {_id: 0 })
db.companies.find( { company: { $regex: /MongoDB/ } }, {_id: 0 } )

返回相同的结果:

[
{ company: "MongoDB" },
{ company: "MongoDB2" }
]
← 比较查询操作符
$gt →