{ field_name: { $not: { $lt: 100 } } }
How do I write the above using MongoDB aggregation language?
I cannot use:
{ $gte: ["$field_name", 100] }
because field_name
may be missing. When it is missing, then it should always be true. This is the reason why I use $not
.
The should be able to use indexes on field_name
2
Answers
$not
with$gt
(not greater than) is equivalent to$lte
(lesser or equal to).While you can use the
$ifNull
operator to define the default value if the field isnull
or missing.Demo @ Mongo Playground
What is your problem? It should just work:
Index is used in either cases.
$match documentation: