Docs 菜单
Docs 主页
/ /

$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> 是正则表达式,MongoDB 会扩展该声明以使用 $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 数组等于指定数组或包含等于数组 [ "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" }
]

后退

对比

在此页面上