Reputation: 35
I have the data as following documents...
[
{
"_id":"5e93193f755fc5e74beb5e8f",
"orgs":[
{
"type":"NonEmployee",
"roles":[]
},
{
"type":"Founder",
"roles":[
{
"_id":"5e44fd4546b3954e6930e83d",
"name":"ADMIN"
},
{
"_id":"5e44fd4546b3954e6930e83e",
"name":"EMPLOYEE"
}
]
}
]
}
]
The query which I am interested in is, Filter Documents based on "OR" conditions of the following...
The sub-query in aggregate looks something like this,
{
$match: {
$expr: {
$or: [
{
$in: ["$orgs.roles.name", rolesArray]
},
{
$eq: ['$orgs.type', 'NonEmployee']
}
]
}
}
}
This gives result as an empty array, because query is incorrect. Is there any possible way to achieve this?
Upvotes: 1
Views: 1279
Reputation: 14287
The query which I am interested in is, Filter Documents based on "OR" conditions of the following...
- If it has type as NonEmployee
- If it has role in given Array eg.rolesArray = ['EMPLOYEE']
The aggregation query using $match
stage and the operators $or
and $in
:
ROLES_ARRAY = [ "EMPLOYEE", "OTHER" ]
db.collction.aggregate( [
{
$match: {
$or: [ { "orgs.roles.name": { $in: ROLES_ARRAY } }, { "orgs.type": "NonEmployee" } ]
}
}
] )
The same query using the find
method:
db.collection.find( { $or: [ { "orgs.roles.name": { $in: ROLES_ARRAY } }, { "orgs.type": "NonEmployee" } ] } )
Upvotes: 0
Reputation: 588
You can query it with elematch instead that matches all the specified query criteria.
You can refer to the doc: https://docs.mongodb.com/manual/reference/operator/query/elemMatch/
Upvotes: 1