Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongodb query subset of an array

I have a field _keywords which is an array of strings. I want to get documents of which _keywords are super-set of the query array.

For example:

db.article.insert({'_keywords': ['foo', 'foo1', 'foo2']})

I want to retrive this record when I query subset of ['foo', 'foo1', 'foo2'], eg: ['foo'], ['foo1', 'foo2']

EDIT: something like:

db.article.find({'_keywords': {$contains: array}})
like image 868
Jensen Avatar asked Aug 31 '12 23:08

Jensen


1 Answers

In MongoDb, for array field:

"$in:[...]" means "intersection" or "any element in",
"$all:[...]" means "subset" or "contain",
"$elemMatch:{...}" means "any element match"
"$not:{$elemMatch:{$nin:[...]}}" means "superset" or "in"
like image 155
diyism Avatar answered Oct 04 '22 14:10

diyism