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?
$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 .
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.
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.”
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With