Rails query of array field with mongoid

calebx · May 31, 2012 · Last by calebx replied at May 31, 2012 · 3548 hits

我的 model 是这样的:

class Trip
  include Mongoid::Document
  include Mongoid::Timestamps
  include Mongoid::MultiParameterAttributes

  field :images, :type => Array, :default => []
end

我的查询是这样:

Trip.where(:images.size => 1).size #=>1, 发现一个array size = 3 的
Trip.where(:images.size => 0).size #=>0, 奇怪的很,什么也没发现。

我想查询 images 这个 array field 为空的记录,应该怎么查呢? 非空的又如何查呢?

mongoid 3.0 好像可以用一个 with_size 方法,但是 2.+ 没有,见鬼,旧的 api 又不见了。

You need to Sign in before reply, if you don't have an account, please Sign up first.