“文档” 菜单
文档首页
/
MongoDB 阿特拉斯
/ / / /

等于

在此页面上

  • 定义
  • 语法
  • 选项
  • 评分行为
  • 举例
  • 样本集合
  • 样本索引
  • 基本查询示例
  • 复合示例查询
equals

equals 操作符检查字段是否与您指定的值匹配。equals 支持查询以下数据类型:

  • 布尔

  • ObjectId

  • number,包括 int32int64double

  • 日期

  • string,作为 Atlas Search token 类型进行索引

  • uuid

  • null

您可以使用 equals 操作符查询数组中的布尔值、对象 Id、数字、日期和字符串(作为 token 类型进行索引)。如果数组中的至少一个元素与 equals 操作符中的“值”字段匹配,则 Atlas Search 将文档添加到结果集中。

注意

equals 操作符支持最多 15 位十进制数字。文档或查询中的额外十进制数字可能会导致精度问题或查询不准确。

equals 通过以下语法实现:

{
$search: {
"index": <index name>, // optional, defaults to "default"
"equals": {
"path": "<field-to-search>",
"value": <boolean-value>|<objectId>|<number>|<date>|<string>,
"score": <score-options>
}
}
}

equals 使用以下词条来构造查询:

字段
类型
说明
必需?
path
字符串
要搜索的索引字段。
value
booleanobjectIdnumberdatestringuuid或 null 。
要查询的值。
score
对象

分配给匹配搜索词结果的分数。使用以下选项之一修改分数:

  • boost将生成的分数乘以给定数字。

  • constant将结果分数替换为给定数字。

  • function:使用给定的表达式替换结果分数。

有关在查询中使用 score 的信息,请参阅对结果中的文档进行评分

没有

默认情况下,equals 使用 constant 评分。每个匹配文档的得分为 1

当您查询数组中的值时,Atlas Search 不会根据与查询匹配的数组内的值数量更改匹配结果的分数。无论数组内有多少个匹配项,分数都将与单个匹配项相同。

有关评分示例,请参阅示例部分。

本页上的示例使用名为 users 的集合,其中包含以下三个文档:

注意

以下示例使用JavaScript格式。 您可以使用mongosh以这种格式添加这些文档。 Atlas 用户界面需要 JSON格式。

db.users.insertMany([
{
"_id" : ObjectId("5ed698faa1199b471010d70c"),
"name" : "Jim Hall",
"verified_user" : true,
"account" : {
"new_user" : true,
"active_user" : true
},
"teammates" : [
ObjectId("5ed6990aa1199b471010d70d"),
ObjectId("59b99dbdcfa9a34dcd7885c8")
],
"region" : "East",
"account_created" : ISODate("2021-12-12T10:18:27.000+00:00"),
"employee_number" : 257
"uuid" : UUID("fac32260-b511-4c69-8485-a2be5b7dda9e")
},
{
"_id" : ObjectId("5ed6990aa1199b471010d70d"),
"name" : "Ellen Smith",
"verified_user" : true,
"account" : {
"new_user" : false,
"active_user" : true
},
"teammates" : [
ObjectId("5a9427648b0beebeb69537a5"),
ObjectId("59b99dbdcfa9a34dcd7881d1")
],
"region" : "Southwest",
"account_created" : ISODate("2022-05-04T05:01:08.000+00:00"),
"employee_number" : 258
},
{
"_id" : ObjectId("5ed6994fa1199b471010d70e"),
"name" : "Fred Osgood",
"verified_user" : false,
"account" : {
"new_user" : false,
"active_user" : false
},
"teammates" : [
ObjectId("5a9427648b0beebeb69589a1"),
ObjectId("59b99dbdcfa9a34dcd7897d3")
],
"region" : "Northwest",
"account_created" : ISODate("2022-01-19T08:22:15.000+00:00"),
"employee_number" : 259
}
])
{
"_id" : ObjectId("5ed698faa1199b471010d70c"),
"name" : "Jim Hall",
"verified_user" : true,
"account" : {
"new_user" : true,
"active_user" : true
},
"teammates" : [
ObjectId("5ed6990aa1199b471010d70d"),
ObjectId("59b99dbdcfa9a34dcd7885c8")
],
"region" : "East",
"account_created" : ISODate("2021-12-12T10:18:27.000+00:00"),
"employee_number" : 257
}
{
"_id" : ObjectId("5ed6990aa1199b471010d70d"),
"name" : "Ellen Smith",
"verified_user" : true,
"account" : {
"new_user" : false,
"active_user" : true
},
"teammates" : [
ObjectId("5a9427648b0beebeb69537a5"),
ObjectId("59b99dbdcfa9a34dcd7881d1")
],
"region" : "Southwest",
"account_created" : ISODate("2022-05-04T05:01:08.000+00:00"),
"employee_number" : 258
}
{
"_id" : ObjectId("5ed6994fa1199b471010d70e"),
"name" : "Fred Osgood",
"verified_user" : false,
"account" : {
"new_user" : false,
"active_user" : false
},
"teammates" : [
ObjectId("5a9427648b0beebeb69589a1"),
ObjectId("59b99dbdcfa9a34dcd7897d3")
],
"region" : "Northwest",
"account_created" : ISODate("2022-01-19T08:22:15.000+00:00"),
"employee_number" : 259
}

users 集合使用以下索引定义进行索引:

{
"mappings": {
"dynamic": true,
"fields": {
"name": {
"type": "token",
"normalizer": "lowercase"
}
}
}
}

该索引定义指定以下内容:

  • 自动对所有可动态索引的字段进行索引。

  • name 字段作为 token 类型进行索引,以支持使用 equals 操作符搜索字符串。

要了解如何创建 Atlas Search 索引,请参阅创建 Atlas Search 索引

以下示例使用 equals 操作符在 users 集合中搜索 verified_user 字段设置为 true 的文档。

db.users.aggregate([
{
"$search": {
"equals": {
"path": "verified_user",
"value": true
}
}
},
{
"$project": {
"name": 1,
"_id": 0,
"score": { "$meta": "searchScore" }
}
}
])

以上查询返回以下结果:

{ "name" : "Jim Hall", "score" : 1 }
{ "name" : "Ellen Smith", "score" : 1 }

“Jim Hall”和“Ellen Smith”的文档得分均为 1,因为这些文档将 verified_user 字段设置为 true

以下示例使用 equals 操作符在 users 集合中搜索包含布尔值 trueaccount.new_user 字段的文档。

db.users.aggregate([
{
"$search": {
"equals": {
"path": "account.new_user",
"value": true
}
}
}
])

前面的查询返回“Jim Hall”的文档,因为该文档在 account 对象中包含 "new_user": true

以下示例使用 equals 操作符在 users 集合中搜索 teammates 字段包含 ObjectId("5a9427648b0beebeb69589a1") 值的文档。

db.users.aggregate([
{
"$search": {
"equals": {
"path": "teammates",
"value": ObjectId("5a9427648b0beebeb69589a1")
}
}
}
])

前面的查询返回“Fred Osgood”的文档,因为该文档在teammates数组中包含ObjectId("5a9427648b0beebeb69589a1")

以下示例使用 equals 操作符在 users 集合中搜索 account_created字段包含与 ISODate("2022-05-04T05:01:08.000+00:00") 匹配的值的文档。

db.users.aggregate([
{
"$search": {
"equals": {
"path": "account_created",
"value": ISODate("2022-05-04T05:01:08.000+00:00")
}
}
}
])

前面的查询返回“Ellen Smith”的文档,因为该文档包含 "account_created": 2022-05-04T05:01:08.000+00:00

以下示例使用 equals 操作符在 users 集合中搜索 employee_number字段包含与 259 匹配的值的文档。

db.users.aggregate([
{
"$search": {
"equals": {
"path": "employee_number",
"value": 259
}
}
}
])

前面的查询返回 "Fred Osgood" 的文档,因为该文档包含"employee_number": 259

以下示例使用 equals 操作符在 users 集合中搜索 name字段包含与 Jim Hall 匹配的值的文档。

db.users.aggregate([
{
"$search": {
"equals": {
"path": "name",
"value": "jim hall"
}
}
}
])

前面的查询返回“Jim Hall”的文档,因为该文档在 name 字段中包含 Jim Hall。Atlas Search 将小写查询与文档中的大写值进行匹配,因为它将词语规范化为小写,如 name 字段的索引定义中指定的一样。

以下示例使用 equals 操作符在 users 集合中搜索 uuid字段包含与特定 UUID 匹配的值的文档。

db.users.aggregate([
{
"$search": {
"equals": {
"path": "uuid",
"value": UUID("fac32260-b511-4c69-8485-a2be5b7dda9e")
}
}
}
])

以下示例使用 equals 操作符在 users 集合中搜索 a 字段包含 null 值的文档。

db.users.aggregate([
{
$search: {
"equals": {
"path": "a",
"value": null
}
}
}
])

以下示例将 compound 操作符与 mustmustNotequals 结合使用,以搜索 region 字段为 Southwest 并且 verified_user 字段不是 false 的文档。

db.users.aggregate([
{
"$search": {
"compound": {
"must": {
"text": {
"path": "region",
"query": "Southwest"
}
},
"mustNot": {
"equals": {
"path": "verified_user",
"value": false
}
}
}
}
}
])

上面的查询返回“Ellen Smith”的文档,这是集合中唯一满足搜索条件的文档。

下面的示例查询具有以下搜索条件:

  • verified_user 字段必须设置为 true

  • 必须满足以下条件之一:

    • teammates 数组包含 ObjectId("5ed6990aa1199b471010d70d")

    • region 字段设置为 Northwest

db.users.aggregate([
{
"$search": {
"compound": {
"must": {
"equals": {
"path": "verified_user",
"value": true
}
},
"should": [
{
"equals": {
"path": "teammates",
"value": ObjectId("5ed6990aa1199b471010d70d")
}
},
{
"text": {
"path": "region",
"query": "Northwest"
}
}
],
"minimumShouldMatch": 1
}
}
},
{
"$project": {
"name": 1,
"_id": 0,
"score": { "$meta": "searchScore" }
}
}
])

以上查询返回以下结果:

{ "name" : "Jim Hall", "score" : 2 }

“Jim Hall”的文档得分为 2,因为它满足 must 子句以及两个 should 子句中的第一个子句的要求。

您可以使用复合查询搜索多个 ObjectID。以下示例查询使用 compound 操作符和 should 子句搜索三个不同的 ObjectID,其中的至少两个 ObjectID 必须满足查询条件。

db.users.aggregate([
{
"$search": {
"compound": {
"should": [
{
"equals": {
"path": "teammates",
"value": ObjectId("5a9427648b0beebeb69537a5")
}
},
{
"equals": {
"path": "teammates",
"value": ObjectId("59b99dbdcfa9a34dcd7881d1")
}
},
{
"equals": {
"path": "teammates",
"value": ObjectId("5a9427648b0beebeb69579d0")
}
}
],
"minimumShouldMatch": 2
}
}
},
{
"$project": {
"name": 1,
"_id": 0,
"score": { "$meta": "searchScore" }
}
}
])

以上查询返回以下结果:

{ "name" : "Ellen Smith", "score" : 2 }

“Ellen Smith”的文档得分为 2,因为它在 teammates 数组中包含两个指定的 ObjectID。

后退

嵌入式文档

来年

存在