I have a MongoDB document with number field:
[
{
_id: 1,
number: 10
},
{
_id: 2,
number: 11
},
{
_id: 3,
number: 12
}
]
And I’m using MongoDB filter $gte
and $lte
it looks like this:
db.collection.aggregate([{$match: {number: {$gte: 10, $lte: 100}}]
And I want to know is there the way to exclude some numbers? For example I have 100 documents with number from 10 to 100, and I do not want to get number 55 but I still need to use $gte
and $lte
. How can I do that?
And now I can split my gte
and lte
numbers and exclude 1 number, but what should I do if I need to exclude 2 or more numbers?
2
Answers
As
@cmgchess
said in the comment section, you are able to exclude number with$ne
and$nin
.$ne
: https://mongoplayground.net/p/q3NxBsUTBoD$nin
: https://mongoplayground.net/p/PGdwhPqKOStDatabase:
$ne
query:$nin
query: