我正在使用Mongoid。我有一个文件,看起来像下面这样:如何在MongoDB文档中使用Mongoid执行此查询
Pet:
foo: {bar: 1, foobar: 2}
another_attr: 1
在这种情况下,最好使用Mongoid,我将如何查询有酒吧值大于0,其中another_attr等于1时所有的宠物吗?
我正在使用Mongoid。我有一个文件,看起来像下面这样:如何在MongoDB文档中使用Mongoid执行此查询
Pet:
foo: {bar: 1, foobar: 2}
another_attr: 1
在这种情况下,最好使用Mongoid,我将如何查询有酒吧值大于0,其中another_attr等于1时所有的宠物吗?
在蒙戈外壳:
db.pets.find({"foo.bar":{$gt:0}, another_attr:1});
在mongoid:
Pet.where(:'foo.bar'.gt => 0, :another_attr => 1)
要找到一间酒吧值大于0所有的宠物,你可以在查询中使用$ GT关键字。要访问一个嵌套场(酒吧),你可以使用点符号:
db.pet.find({"foo.bar" : {$gt : 0}})
然后,还需要another_attr等于1,你可以添加其他的要求进入查询:
db.pet.find({"foo.bar" : {$gt : 0} , another_attr : 1 })
您可以在这里找到有关高级查询的文档:http://www.mongodb.org/display/DOCS/Advanced+Queries