用于深度嵌套JSON的Cloudant/Mango选择器 [英] Cloudant/Mango selector for deeply nested JSONs

查看:99
本文介绍了用于深度嵌套JSON的Cloudant/Mango选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我的一些文档具有以下结构:

Let's say some of my documents have the following structure:

{  
   "something":{  
      "a":"b"
   },
   "some_other_thing":{  
      "c":"d"
   },
   "what_i_want":{  
      "is_down_here":[  
         {  
            "some":{  
               "not_needed":"object"
            },
            "another":{  
               "also_not_needed":"object"
            },
            "i_look_for":"this_tag",
            "tag_properties":{  
               "this":"that"
            }
         },
         {  
            "but_not":{  
               "down":"here"
            }
         }
      ]
   }
}

是否存在可以在"i_look_for"上成功选择值为"this_tag"的Mango JSON选择器?它在数组内部(我知道它在数组中的位置).我对过滤结果也很感兴趣,所以我只得到结果中的"tag_properties".

Is there a Mango JSON selector that can successfully select on "i_look_for" having the value "this_tag" ? It's inside an array (i know its position in the array). I'm also interested on filtering the result so I only get the "tag_properties" in the result.

我尝试了很多事情,包括$ elemMatch,但是所有东西大多返回无效的json".

I have tried a lot of things, including $elemMatch but everything mostly return "invalid json".

这是芒果的用例还是我应该坚持观点?

Is that even a use case for Mango or should I stick with views ?

推荐答案

使用Cloudant Query(Mango)选择器语句,您仍然需要在查询之前定义适当的索引.考虑到这一点,这是您的答案:

With Cloudant Query (Mango) selector statements, you still need to define an appropriate index before querying. With that in mind, here's your answer:

json型CQ索引

{
  "index": {
    "fields": [
      "what_i_want.is_down_here.0"
    ]
  },
  "type": "json"
}

针对json类型索引的选择器

{
  "selector": {
    "what_i_want.is_down_here.0": {
      "i_look_for": "this_tag"
    },
    "what_i_want.is_down_here.0.tag_properties": {
      "$exists": true
    }
  },
  "fields": [
    "_id",
    "what_i_want.is_down_here.0.tag_properties"
  ]
}

上面的解决方案假定您始终知道/可以保证所需的字段在is_down_here数组的第0个元素之内.

The solution above assumes that you always know/can guarantee the fields you want are within the 0th element of the is_down_here array.

还有另一种方法可以使用不同的CQ索引类型来回答此问题. 本文介绍了差异,并提供了一些有用的示例来显示查询数组.现在,您对不同的索引类型有了更多的了解,下面是使用Lucene搜索/文本"类型的CQ索引来回答问题的方法:

There is another way to answer this question with a different CQ index type. This article explains the differences, and has helpful examples that show querying arrays. Now that you know a little more about the different index types, here's how you'd answer your question with a Lucene search/"text"-type CQ index:

文本型CQ索引

{
  "index": {
    "fields": [
      {"name": "what_i_want.is_down_here.[]", "type": "string"}
    ]
  },
  "type": "text"
}

针对文本类型索引的选择器

{
  "selector": {
    "what_i_want.is_down_here": {
      "$and": [
        {"$elemMatch": {"i_look_for": "this_tag"}},
        {"$elemMatch": {"tag_properties": {"$exists": true}}}
      ]
    }
  },
  "fields": [
    "_id",
    "what_i_want.is_down_here"
  ]
}

阅读文章,您将了解每种方法都有其权衡因素:json类型的索引更小,更不灵活(只能索引特定元素);文本类型更大但更灵活(可以索引所有数组元素).从这个示例中,您还可以看到,投影值还具有一些折衷(投影特定值与整个数组).

Read the article and you'll learn that each approach has its tradeoffs: json-type indexes are smaller and less flexible (can only index specific elements); text-type is larger but more flexible (can index all array elements). And from this example, you can also see that the projected values also come with some tradeoffs (projecting specific values vs. the entire array).

这些线程中的更多示例:

More examples in these threads:

  • Cloudant Selector Query
  • How to index multidimensional arrays in couchdb

这篇关于用于深度嵌套JSON的Cloudant/Mango选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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