My records each have the following attributes of type Array:
alpha, beta, gamma, delta, epsilon, zeta
I know I can find records that have at least one component in an array, like this:
db.accommodations.find({'alpha.1': {$exists: true}})
I want to be able to find records that have at least one component in at least 3 arrays. For example, this record could be found:
alpha = []
beta = [1, 1, 1]
gamma = []
delta = [1, 1]
epsilon = []
zeta = [1]
But this could not:
alpha = []
beta = [1, 1, 1]
gamma = []
delta = [1, 1]
epsilon = []
zeta = []
Is there any way I can do this?
2
Answers
Maybe something like this:
Explained:
$match if the sum() on non empty arrays is >=3
where empty=0 and non-empty=1
playground
Query
$size>=1
(
$size
is more general, if you only care for at least 1, you can also do$ne
with[]
)$size>=3
document passesPlayMongo