关于n1ql:如何在Couchbase中的对象内添加索引和查询数组?

How to add index and query array within object in couchbase?

具有多个文档,其结构类似于以下内容:

1
2
3
4
5
6
7
8
{
'key1':value,
'key2':value2,
'galaxy':{
           'planets':['earth','mars','welcomebackpluto']
           'sun': 'something'
          }
}

我想查询在galaxy对象的planets数组中具有mars的所有文档。

我创建的索引没有用,我无法查询它们,不知道该怎么做?


使用数组索引

1
2
3
4
5
CREATE INDEX ix1 ON default (DISTINCT ARRAY (DISTINCT ARRAY p FOR p IN g.planets END) FOR g IN galaxy END) WHERE type ="xyz";

SELECT d.*
FROM default AS d
WHERE d.type ="xyz" AND ANY g IN d.galaxy SATISFIES (ANY p IN g.planets SATISFIES p ="mars" END) END;

https://docs.couchbase.com/server/6.0/n1ql/n1ql-language-reference/indexing-arrays.html