Reputation: 5210
I have a set of records like the following:
[
{ title: 'foo', members: [ 'a', 'b' ] },
{ title: 'bar', members: [] },
{ title: 'fiz', members: [ 'b' ] }
]
The members
prop is used for restricting certain records so what I'm hoping to do is check the property and if empty or doesn't exist OR it contains the member return the item.
For example:
a
would get back foo
(is a member) and bar
(no member restrictions)b
would get all three because they are a member on 0
and 2
and no member restrictions are set on on 1
Upvotes: 3
Views: 165
Reputation: 13103
Something like this:
db.collection.find({
$or: [
{
"members.0": {
$exists: false
}
},
{
"members": "User"
}
]
})
or
db.collection.find({
$or: [
{
$expr: {
$eq: [
{
$size: "$members"
},
0
]
}
},
{
"members": "User"
}
]
})
Upvotes: 3