$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> } }。
安全影响
始终将显式格式 { 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" } ]