How would we go about finding records where multiple conditions are true within the same sub-document while at least one of these conditions is negated?
db.getCollection('clients').find( {
data: { '$exists': true },
'data.updates': { '$elemMatch': {
name: { $not: /^KB3109103/i },
install_date: { $gt: 128573812 }
} }
});
This returns all records because $not
doesn't seem to work inside $elemMatch
.
Solution:
Found a work around, adding $and (which according to the documentation is the same as without) solved it.
db.getCollection('clients').find( {
data: { '$exists': true },
'data.updates': { '$elemMatch': {
$and: [
{name: { $not: /^KB3109103/i }},
{install_date: { $gt: 128573812 }}
]
} }
});