使用Cloudant Query(Mango)选择器语句,您仍然需要在查询之前定义适当的索引。考虑到这一点,这是您的答案:
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个元素内。
还有另一种方法可以使用不同的CQ索引类型来回答此问题。本文介绍了这些差异,并提供了一些有用的示例来显示查询数组。现在,您对不同的索引类型有了更多的了解,下面是使用Lucene搜索/“文本”类型的CQ索引回答问题的方法:
文字型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类型的索引更小,更不灵活(只能索引特定元素);文本类型更大但更灵活(可以索引所有数组元素)。从该示例中,您还可以看到,投影值还具有一些折衷(投影特定值与整个数组)。



