我有这样的模型
class User
include Mongoid::Document
field :c, as: :categories, type: Array
end
我正在这样存储信息:
a = UserCheckin.new
a.c = [{id: rand(1000), name: 'a'}, {id: rand(1000), name: 'b'}, {id: rand(1000), name: 'c'}]
a.save
我不知道我是否通过在其上存储哈希来滥用数组类型,但问题是mongodb并没有抱怨它。
如何查询类别名称为'a'或类别ID高于2的用户?
提前致谢,
答案 0 :(得分:12)
我似乎找到了答案......对于任何人,我会在这里发布。
User.where(c: {'$elemMatch' => {name: 'a'}})
它将返回所有用户,其categories数组有一个或多个名为'a'的元素。