如何在CouchDB中索引多维数组

编程入门 行业动态 更新时间:2024-10-11 07:33:51
本文介绍了如何在CouchDB中索引多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个多维数组,我想用CouchDB索引(确实使用Cloudant).我有一些用户,其中包含他们所属的团队的列表.我想搜索找到该团队的每个成员.因此,请获取具有ID为79d25d41d991890350af672e0b76faed的团队对象的所有User对象.我试图在"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", "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版本中称为"Mango"),而不是CouchDB中的传统MapReduce视图索引系统. (此博客涵盖了以下差异: cloudant/blog/mango-json-vs-text-indexes/,这是一个概述: developer.ibm/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: cloudant/blog/mango-json-vs-text-indexes/ and this one is an overview: developer.ibm/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"]}' broberg.cloudant/teams_test/_find

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

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

挖出Seinfeld主题:D

Dig the Seinfeld theme :D

更多推荐

如何在CouchDB中索引多维数组

本文发布于:2023-11-30 15:53:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1650589.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多维   数组   索引   如何在   CouchDB

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!