mongodb, how to find in array other array -
i need help. trying find array in array.
records:
{ "_id" : objectid("56b7e6966213e8d142ef55b7"), "users" : [ objectid("56b0e547a838b5a45b4d0100"), // 0 objectid("56a7e16e37e5adf32b97cc3d") // 1 ] }, { "_id" : objectid("56b7e6966213e8d142ef55b7"), "users" : [ objectid("56b0e547a838b5a45b4d0100"), objectid("56a7e16e37e5adf32b97cc3d"), objectid("56b7e6b96213e8d142ef55b8") ] } i'm trying find first record "_id" : objectid("56b7e6966213e8d142ef55b7"),
i'm using query:
db.collection.find({"users" : { $in: [ objectid("56b0e547a838b5a45b4d0100"), objectid("56a7e16e37e5adf32b97cc3d") ]}}) or
db.collection.find({ $and: [{ "users": objectid("56b0e547a838b5a45b4d0100") }, { "users": objectid("56a7e16e37e5adf32b97cc3d") }]}) response gives me 2 records.
this request gives me correct response:
db.collection.find({"users" : [ objectid("56b0e547a838b5a45b4d0100"), // 0 objectid("56a7e16e37e5adf32b97cc3d") // 1 ]}) but if record has array this:
{ "_id" : objectid("56b7e6966213e8d142ef55b7"), "users" : [ objectid("56a7e16e37e5adf32b97cc3d"), // 1 objectid("56b0e547a838b5a45b4d0100") // 0 ] } it doesn't work
$all, $eq, $in doesn't work
db.rooms.find({ users: { $all: [ objectid("56a7e16e37e5adf32b97cc3d"), objectid("56b0e547a838b5a45b4d0100") ] } }) i need find rows [1,2] == [1,2] or [1,2] == [2,1]
not [1,2,3] == [1,2]
i appreciate if can me.
if expect resulting document "only" 2 array entries, combination of conditions want $all , $size:
db.rooms.find({ "users": { "$all": [ objectid("56b0e547a838b5a45b4d0100"), objectid("56a7e16e37e5adf32b97cc3d") ], "$size": 2 } }) where $all $and shorter syntax, , $size retricts since list n entries, want match of particular length/size.
this better specifying array directly, since match in either order, opposed statement not match considering order different , therefore not "exact" match:
db.rooms.find({ "users": [ objectid("56a7e16e37e5adf32b97cc3d"), objectid("56b0e547a838b5a45b4d0100") ] }) so logically array contains "all" specified entries, , same "size" input , no different.
Comments
Post a Comment