mongodb - 图集搜索对象数组

标签 mongodb mongodb-atlas mongodb-atlas-search

我有以下架构:

{
   name: String,
   phones: [
        {
            number: String,
            type: String
        }
   ]
}

如何为 phones.number 编制索引以便我可以编写如下内容:

collection.aggregate([{
       "$search":{ 
            "compound":{
                  "should":[
                      {"autocomplete":{"query":"012345","path":"name"}},
                      {"autocomplete":{"query":"012345","path":"phones.number"}}
                  ]
             }
         }
}])

文档 here给出一个字符串数组而不是对象数组的例子。

最佳答案

根据 this answer , 支持按数组中子文档的属性进行索引。只需通过 phones.number 创建一个索引。

参见 the documentation获取更多信息。


编辑

我混淆了标准索引和 Atlas Search 索引。从文档中,您应该能够以这种方式索引文档数组:


{
  "analyzer":"lucene.standard",
  "searchAnalyzer":"lucene.standard",
  "mappings":{
    "dynamic":false,
    "fields":{
      "name":{
        "type":"string",
        "analyzer":"lucene.standard"
      },
      "phones":{
        "type":"document",
        "fields":{
          "number":{
            "type":"string",
            "analyzer":"lucene.standard"
          },
          "type":{
            "type":"string",
            "analyzer":"lucene.standard"
          }
        }
      }
    }
  }
}

关于mongodb - 图集搜索对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65532707/

相关文章:

node.js - AWS elastic beanstalk Node.js 应用程序未连接到 Mongodb atlas

node.js - Loopback - 无法使用 mongodb+srv 协议(protocol)从环回连接到 Mongo DB Atlas

mongodb - 如何使用复合 MongoDB Atlas 搜索查询在整数数组中进行搜索?

mongoose - MongoDB Atlas 按引用文档字段搜索词搜索

c++ define指令失败,代码看起来不错

javascript - 无法从 javascript 打印 BSON 对象

node.js - mongoDB 4.0/指南针: No hostname or hostnames provided in connection string

MongoDB Atlas 搜索 - 搜索字符串中的多个术语具有 'and' 条件(不是 'or' )

javascript - 如何更新 Mongoose 嵌套数组文档

python - 在 pymongo 中使用 $natural sort - MongoDB