mongodb query subset of an array

后端 未结 3 1257
清酒与你
清酒与你 2021-02-06 10:03

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

相关标签:
3条回答
  • 2021-02-06 10:49

    Short answer: $all operator.
    To query documents with array with superset of ['foo1', 'foo2'], use:
    db.article.find( { '_keywords': { $all: ['foo1', 'foo2'] } } );

    0 讨论(0)
  • 2021-02-06 10:53

    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"
    
    0 讨论(0)
  • 2021-02-06 10:55

    Use the $all operator:

    db.article.find( { _keywords: { $all: [ 'foo1', 'foo2' ] } } );
    

    Source: http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-%24all

    0 讨论(0)
提交回复
热议问题