在MongoDb中查询嵌套数组

3

我希望能够通过嵌套数组中的字符串来检索文档。例如,数据(表示句子的依赖解析)如下:

{'tuples': [['xcomp', 'multiply', 'using'], 
            ['det', 'method', 'the'], 
            ['nn', 'method', 'foil'], 
            ['dobj', 'using', 'method']]}

我找到的最接近的解决方案假定['nn', ...]tuples列表嵌套列表的第二个位置。
 db.c.find({'tuples.2.0' : 'nn'})

有没有一种方法可以放松固定位置?元组(而不是它们的内容)可以以任何顺序出现。

其次,能够检索具有['nn','method',X]的文档将非常好,意味着在依赖关系解析中有一个名词“method”。

谢谢!

1个回答

4

明白了!

db.c.find({'tuples' : {$elemMatch : {$all : ['nn']}}})
db.c.find({'tuples' : {$elemMatch : {$all : ['nn','method']}}})

即使你知道“nn”在第三行,但不知道它在什么位置,你仍然需要使用$all。根据find的文档,你不应该这样做。在mongo shell中尝试这个:db.nested.insert({'level1': {'level2': [['item00', 'item01'], ['item10', 'item11']]}})。然后,执行 db.nested.findOne({'level1.level2.0': 'item00'})。为什么呢? - dgorur

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接