What does $nor do in Mongoose?
Finds documents where none of the conditions match. User.find({ $nor: [{ role: 'admin' }, { role: 'moderator' }] }) finds users who are neither admin nor moderator. Useful for exclusion queries.
Verify This Answer
Cross-check this information using these trusted sources:
More FAQs in Logical Query Building in MongoDB With Mongoose
$and takes an array of conditions; all must match. $or takes an array; any must match. Combine them: $and: [{ status: 'active' }, { $or: [{ age: { $lt: 18 } }, { age: { $gt: 65 } }] }] for active users under 18 or over 65.
When you need the same field twice with different operators, or to group $or conditions with other conditions. For most queries, passing multiple fields in one object ANDs them implicitly. Use $and only when implicit AND is not enough.
Start with an empty filter object. For each query param that is present, add it to the filter. For range queries (minAge, maxAge), build an age object with $gte and $lte. Pass the filter to User.find(filter).
Still have questions?
Browse all our FAQs or reach out to our support team
