Mongoid query for array field

前端 未结 4 730
余生分开走
余生分开走 2021-02-01 22:59

I have a category field of type Array in Mongoid.

Ex. category: [\"val1\",\"val2\",\"val3\"]

相关标签:
4条回答
  • 2021-02-01 23:09

    You can use Criteria all_in to make it simpler:

    Model.all_in(category: ['val1','val2'])
    
    0 讨论(0)
  • 2021-02-01 23:13

    This worked

    Model.where(:category.in => ['val1','val2'])
    

    From Mongo Docs

    0 讨论(0)
  • 2021-02-01 23:16

    In mongoid, there is '$in' operator. So you can do this :

    Model.where(category: { '$in': ['val1', 'val2'] })
    
    0 讨论(0)
  • 2021-02-01 23:24

    Or another variant:

    Model.in(category: ['val1','val2'])
    
    0 讨论(0)
提交回复
热议问题