Mike P
Mike P

Reputation: 2877

Querying array of arrays in MongoDB

I have a mongo collection which has an array of arrays (bigrams from a NLP process) that I'd like to be able to search, so for example;

{
    "sentence" : "will most likely be",
    "biGrams" : [["will","most"], ["most","likely"], ["likely", "be"]
},
{
    "sentence" : "likely most people use stackoverflow",
    "biGrams" : [["likely","most"], ["most","people"], ["people", "use"], ["use", "stackoverflow"]
}

What I'd like to be able to do is search through the biGram sub-doucment for a certain instance of one of these bigrams, e.g. search for all sentences that contain the bigram ["most","likely"].

I've tried this;

find({'biGrams':{$elemMatch: {$elemMatch:{$in:['most','likely']}} }})

But this obviously finds all cases with the word 'most' or 'likely' are present. And the order is important, i.e. I don't want to find docs with ['likely','most'] in this example.

Thanks in advance, I'm stumped....

Upvotes: 0

Views: 121

Answers (1)

drmirror
drmirror

Reputation: 3760

How about

find({"biGrams":["most","likely"]})

Searching on a particular field "unwinds" one level of arrays in that field, and searching for a particular array should be a binary match on that array.

Upvotes: 1

Related Questions