mongodb - How to query a collection that matches the exact fields of objects in array -
how query collection matches exact fields of objects in array?
since test case more explicit, here test pass.
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 find invitation bbb , ccc player_id of guests, regardless order. result = invitation.collection.find(...) assert_equal result, [ a, b ]
my use case invitation system same combination of guests can't exist, when new invitation sent, need check if 1 exact same guests (regardless order).
note: use array of guest objects since carry additional data. here example data set (https://gist.github.com/anonymous/5507735).
based on answer nyde1319; feels hackish, since there's no other answers yet here goes:
db.invitation.find({'guests.player':{'$all':['bbb','ccc']}, guests: {$size: 2}})
the number 2 in {$size: 2}
of course depends on length of array.
Comments
Post a Comment