如何在 couchdb 中索引多维数组 [英] How to index multidimensional arrays in couchdb

查看:50
本文介绍了如何在 couchdb 中索引多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个多维数组,我想用 CouchDB 编制索引(实际上是使用 Cloudant).我的用户拥有他们所属团队的列表.我想搜索以找到该团队的每个成员.所以,给我所有的用户对象,他们有一个 id 为 79d25d41d991890350af672e0b76faed 的团队对象.我试图在Teams.id"上创建一个 json 索引,但没有成功,因为它不是一个直数组,而是一个多维数组.

I have a multidimensional array that I want to index with CouchDB (really using Cloudant). I have users which have a list of the teams that they belong to. I want to search to find every member of that team. So, get me all the User objects that have a team object with id 79d25d41d991890350af672e0b76faed. I tried to make a json index on "Teams.id", but it didn't work because it isn't a straight array but a multidimensional array.

用户

{
 "_id": "683be6c086381d3edc8905dc9e948da8",
 "_rev": "238-963e54ab838935f82f54e834f501dd99",
 "type": "Feature",
 "Kind": "Profile",
 "Email": "gc@gmail.com",
 "FirstName": "George",
 "LastName": "Castanza",
 "Teams": [
  {
   "id": "79d25d41d991890350af672e0b76faed",
   "name": "First Team",
   "level": "123"
  },
  {
   "id": "e500c1bf691b9cfc99f05634da80b6d1",
   "name": "Second Team Name",
   "level": ""
  },
  {
   "id": "4645e8a4958421f7d843d9b34c4cd9fe",
   "name": "Third Team Name",
   "level": "123"
  }
 ],
 "LastTeam": "79d25d41d991890350af672e0b76faed"
}

推荐答案

这很像我在 Cloudant 选择器查询 但这是适用于您的问题的交易:

This is a lot like my response at Cloudant Selector Query but here's the deal, applied to your question:

运行此查询的最简单方法是使用Cloudant Query"(或Mango",在即将发布的 CouchDB 2.0 版本中称为)——而不是 CouchDB 中传统的 MapReduce 视图索引系统.(此博客涵盖了差异:https://cloudant.com/blog/mango-json-vs-text-indexes/ 这是一个概述:https://developer.ibm.com/clouddataservices/2015/11/24/cloudant-query-json-index-arrays/).

The easiest way to run this query is using "Cloudant Query" (or "Mango", as it's called in the forthcoming CouchDB 2.0 release) -- and not the traditional MapReduce view indexing system in CouchDB. (This blog covers the differences: https://cloudant.com/blog/mango-json-vs-text-indexes/ and this one is an overview: https://developer.ibm.com/clouddataservices/2015/11/24/cloudant-query-json-index-arrays/).

你的 CQ 索引应该是这样的:

Here's what your CQ index should look like:

{
  "index": {
    "fields": [
      {"name": "Teams.[].id", "type": "string"}
    ]
  },
  "type": "text"
}

以及后续查询的样子:

{
  "selector": {
    "Teams": {"$elemMatch": {"id": "79d25d41d991890350af672e0b76faed"}}
  },
  "fields": [
    "_id",
    "FirstName",
    "LastName"
  ]
}

您可以在 Cloudant 仪表板的查询"部分自己尝试,也可以通过 curl 使用类似以下内容:

You can try it yourself in the "Query" section of the Cloudant dashboard or via curl with something like this:

curl -H "Content-Type: application/json" -X POST -d '{"selector":{"Teams":{"$elemMatch":{"id":"79d25d41d991890350af672e0b76faed"}}},"fields":["_id","FirstName","LastName"]}' https://broberg.cloudant.com/teams_test/_find

该数据库是全球可读的,因此您可以在此处查看我在其中创建的示例文档:https://broberg.cloudant.com/teams_test/_all_docs?include_docs=true

That database is world-readable, so you can see the sample documents I created in there here: https://broberg.cloudant.com/teams_test/_all_docs?include_docs=true

挖掘宋飞主题:D

这篇关于如何在 couchdb 中索引多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆