使用 cloudant 和 couchdb 2.0 的芒果查询在数组中索引和查询项目

index and query items in an array with mango query for cloudant and couchdb 2.0

我有以下数据库结构:

{"_id": "0096874","genre": ["Adventure","Comedy", "Sci-Fi" ]}
{"_id": "0099088","genre": ["Comedy", "Sci-Fi", "Western"]}

并想像在 mongodb

中那样查询它
db.movies.find({genre: {$in: ["Comedy"]}})

当我为整个数据库使用文本索引时它起作用了,但这看起来很浪费:

// index
    {
      "index": {},
      "type": "text"
    }
//query
{
  "selector": {
    "genre": {
      "$in": ["Comedy"]
    }
  },
  "fields": [
    "_id",
    "genre"
  ]
}

以下索引不起作用:

{
  "index": {
    "fields": [
      "genre"
    ]
  },
  "type": "json"
}

不为整个数据库编制索引的 cloudant 查询的正确索引是什么? 感谢您的帮助

你几乎是正确的。您的索引是正确的,但是您需要输入一个选择器来获取所有 ID https://cloudant.com/blog/mango-json-vs-text-indexes/

正如托尼所说,这不是一个很好的性能解决方案,

之所以可行,是因为 Mango 执行上述 $in 操作作为针对​​所有文档的过滤机制。正如我们在上一节关于 JSON 语法的结论中看到的那样,上面查询的性能权衡是它本质上执行完整索引扫描然后应用过滤器。

{
  "selector": {
    "_id": {
      "$gt": null
    }, 
    "genre": {
      "$in": ["Western"]
    }
  },
  "fields": [
    "_id",
    "genre"
  ],
  "sort": [
    {
      "_id": "asc"
    }
  ]
}