Query a nested array in MongoDb

冷暖自知 提交于 2019-12-12 15:55:06

问题


I would like to retrieve documents by the presence of an string in a nested array. For example, the data (representing a dependency parse of a sentence) looks like:

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

The closest solution I've found assumes that ['nn', ...] is the second position of the tuples list-of-lists:

 db.c.find({'tuples.2.0' : 'nn'})

Is there a way to relax the fixed position? The tuples (not their contents) can be in any order.

Secondly, it would be really great to be able to retrieve documents that have ['nn', 'method', X], meaning a noun "method" in their dependency parse.

Thank you!


回答1:


Got it!

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


来源:https://stackoverflow.com/questions/5250652/query-a-nested-array-in-mongodb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!