2014-09-24 65 views
0

有沒有辦法在Mongoid中的嵌入式文檔中找到零值?如何在Mongoid中找到無字段值的嵌入式文檔?

鑑於我有這些模型:

class Record 
    include Mongoid::Document 

    embeds_many :locations 
end 

class Location 
    include Mongoid::Document 

    embedded_in :record 

    field :special_id, type: String 
end 

我可以找到特定地點模型

Record.where('locations.special_id' => '123') 

的具體special_id記錄,但,如果我想要得到的位置與零special_id的所有記錄,這有效,但會返回所有記錄。

Record.where('locations.special_id.eq' => nil) 

這一次返回0結果:

Record.where('locations.special_id.exists' => false) 

感謝

+0

也許http://stackoverflow.com/a/8963075/1197775 – juanpastas 2014-09-24 05:02:26

+0

@juanpastas這就是如果你沒有嵌入文件。如果'special_id'是'record'模型的一個字段,那麼這將起作用。 – Ben 2014-09-24 05:04:27

+1

'Record.where('locations.special_id'=> nil)''? – 2014-09-24 05:19:27

回答

0

embeds_many剛剛建立內部MongoDB的哈希值的數組所以平時點符號應該工作:

Record.where('locations.special_id' => nil) 
# ------------^^^^^^^^^^^^^^^^^^^^ just the usual JavaScript-style path 

.eq.exists notatio ns通常用作符號的簡寫形式。例如:

where(:'locations.special_id'.ne => nil) 

將是一個速記:

where('locations.special_id' => { :$ne => nil }) 

如果你嘗試嵌入的字段名字符串,在操作者的MongoDB將只是覺得你想在使用其他組件路徑。