【问题标题】:index and query items in an array with mango query for cloudant and couchdb 2.0使用 Cloudant 和 couchdb 2.0 的芒果查询索引和查询数组中的项目
【发布时间】:2016-03-10 12:10:06
【问题描述】:

我有以下数据库结构:

{"_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 查询的正确索引,它不索引整个数据库? 感谢您的帮助

【问题讨论】:

    标签: couchdb cloudant couchdb-mango


    【解决方案1】:

    你说得几乎是正确的。您的索引是正确的,但是您需要输入一个选择器来获取所有 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"
        }
      ]
    }
    

    【讨论】:

    • 可惜芒果需要做一个过滤操作。在 mongodb 中,可以在数组上放置一个真正的索引。难道不能在 couchdb 中做一个简单的映射,所有数组成员都是单独发出的吗? (doc) -> 如果 doc.genre 则 doc.genre.forEach (item) -> 发出项目。只是我的 2 克拉。
    • 那么我怎样才能在类似数组的值上使用替代方案和更高的性能呢?我看不到为此使用 cloudants 新索引方法的方法我不确定在哪里放置类似 gist.github.com/tannerlinsley/34d6f3511efd2a74ec49 的内容
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多