Mongodb Native Query -
my data mongodb
{ "_id" : objectid("57d718ddd4c618cbf04772d6"), "_class" : "io.core.entity.layer", "name" : "u2", "layermembers" : [ { "permission" : "owner", "user" : { "_id" : objectid("57d440c3d4c60e2f13553216"), "namesurname" : "user 2", "email" : "user2@email.com" }, "isowner" : true }, { "permission" : "edit", "user" : { "_id" : objectid("57d44050d4c62bfdc8a9fd30"), "namesurname" : "user 1", "email" : "user@email.com" }, "isowner" : false } ] }
my queries;
db.getcollection('layer').find({$and: [{"layermembers.user._id": objectid("57d440c3d4c60e2f13553216"), "layermembers.permission": "edit"}]}) db.getcollection('layer').find({$and: [{"layermembers.user._id": objectid("57d440c3d4c60e2f13553216"), "layermembers.isowner": false}]})
these queries, both of them found data, in opinion, should not data. because query 'and' query , when user id equals "57d440c3d4c60e2f13553216", permission "owner" , "layermembers.isowner" true.
and query can find data.
db.getcollection('layer').find({"layermembers.user._id": objectid("57d440c3d4c60e2f13553216"), "layermembers.isowner": false})
what missing part ?
you should use $elemmatch (https://docs.mongodb.com/manual/reference/operator/query/elemmatch/) if want return document trying match multiple fields within nested array.
something like:
{ "layermembers": "$elemmatch": { "user._id": objectid("57d440c3d4c60e2f13553216"), "permission": "edit" } }
Comments
Post a Comment