Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongodb having multiple conditions for $ne

Tags:

Im trying to do a query where I exclude certain borough names such as Brooklyn, Queens, and Staten Island, I was able to exclude just one but can not figure out how to exclude more then just one

excluding just one: db.rest.find({ borough : {$ne : "Brookyln"}});

this worked just to exclude one and so I looked around to see how I can do multiple and tried

db.rest.find({ borough : {$ne : ["Brooklyn", "Queens", "Staten Island"]}});

which did not work, how can I accomplish this?

like image 363
goobs Avatar asked May 10 '17 19:05

goobs


People also ask

What is $NE in MongoDB?

$ne selects the documents where the value of the field is not equal to the specified value . This includes documents that do not contain the field .

How do I test multiple values in MongoDB?

MongoDB provides the find() that is used to find multiple values or documents from the collection. The find() method returns a cursor of the result set and prints all the documents. To find the multiple values, we can use the aggregation operations that are provided by MongoDB itself.

How do you write a not equal query in MongoDB?

You can use the $ne operator (which stands for “not equal”) in MongoDB to query for documents where a field is not equal to a certain value. This particular example finds all documents in the collection titled myCollection where the team field is not equal to “Mavs.”


1 Answers

You can use the $nin operator:

db.rest.find({ borough : {$nin : ["Brooklyn", "Queens", "Staten Island"]}}); 

Ref: https://docs.mongodb.com/manual/reference/operator/query/nin/

like image 200
Wesley Monaro Avatar answered Sep 19 '22 16:09

Wesley Monaro