如何查詢與數組中對象的確切字段匹配的集合?如何查詢與數組中對象的確切字段匹配的集合
由於測試用例更加明確,因此這裏是要通過的測試。
a = Invitation.create(guests: [ Guest.new(player: 'bbb'), Guest.new(player: 'ccc') ])
b = Invitation.create(guests: [ Guest.new(player: 'ccc'), Guest.new(player: 'bbb') ])
c = Invitation.create(guests: [ Guest.new(player: 'bbb'), Guest.new(player: 'ccc'), Guest.new(player: 'ddd') ])
# Request to find invitation with bbb and ccc as player_id of guests, regardless the order.
result = Invitation.collection.find(...)
assert_equal result, [ a, b ]
我用例是一個邀請系統,其中的客人相同的組合可以不存在,所以當發送一個新的邀請,我需要檢查,如果一個完全相同的人(不管它們的順序)。
注:我使用一個Guest對象數組,因爲它攜帶一些額外的數據。這裏是一個示例數據集(https://gist.github.com/anonymous/5507735)。
你能提供一個mongo記錄的例子嗎? db.Invitation.findOne()會做的。 – shargors 2013-05-02 19:05:47
我編輯帖子以添加示例數據集(https://gist.github.com/anonymous/5507735)。 – 2013-05-03 07:35:58