0

If my stored document looks like this:

doc = {
    'Things' : [ 'one' , 'two' , 'three' ]
    }

How can I query for documents which contain one in Things?

I know the $in operator queries a document item against a list, but this is kind of the reverse. Any help would be awesome.

1 Answer 1

2

Use MongoDB's multikeys support:

MongoDB provides an interesting "multikey" feature that can automatically index arrays of an object's values.
[...]

db.articles.find( { tags: 'april' } )
{"name" : "Warm Weather" , "author" : "Steve" , 
 "tags" : ["weather","hot","record","april"] , 
 "_id"  : "497ce4051ca9ca6d3efca323"}

Basically, you don't have to worry about the array-ness of Things, MongoDB will take care of that for you; something like this in the MongoDB shell would work:

db.your_collection.find({ Things: 'one' })
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.