3

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!

trope
  • 497
  • 1
  • 5
  • 16

1 Answers1

4

Got it!

db.c.find({'tuples' : {$elemMatch : {$all : ['nn']}}})
db.c.find({'tuples' : {$elemMatch : {$all : ['nn','method']}}})
trope
  • 497
  • 1
  • 5
  • 16
  • Even if you knew that 'nn' was in the third row, but didn't know in what position, you still need the $all. According to documentation for find, you shouldn't. Try this in mongo shell: db.nested.insert({'level1': {'level2': [['item00', 'item01'], ['item10', 'item11']]}}). Then, db.nested.findOne({'level1.level2.0': 'item00'}) Why? – dgorur Mar 25 '13 at 19:31