John_Page
(John Page)
2
In this cse $in woudl only be true if one of the arrays was an element of the other, what you are looking for in an intersection of the set of values in the arrays - so you need $setIntersection
``var data = [
{
_id: 1,
function: 'auth_group',
roles: [ 'superadmin', 'manager' ],
currentUser: { _id: 2, account: 'admin', roles: [ 'administrator', 'staff' ] }
},
{
_id: 3,
function: 'auth_user',
roles: [ 'administrator', 'manager' ],
currentUser: { _id: 4, account: 'admin', roles: [ 'administrator', 'staff' ] }
}
]
db.b.drop()
db.b.insertMany(data)
var pipeline = [
{
"$match": {
"$expr":
{ $ne : [ [],
{ "$setIntersection": ["$roles","$currentUser.roles"]}
]
}
}
}
]
db.b.aggregate(pipeline)