Mongodb find all except from one or two criteria

Diolor picture Diolor · Aug 26, 2013 · Viewed 15.6k times · Source

Alright for one field matching I run:

db.bios.find( { "Country":"Netherlands" } )

How can I bring all documents but not the ones with "Country":"Netherlands"?

Also is it possible to bring all documents but without 2 countries?

Answer

Yevgeniy Anfilofyev picture Yevgeniy Anfilofyev · Aug 26, 2013

Use $nin operator

For example:

db.bios.find( { Country: { $nin: ["Country1", "Country2"] } } )

And $ne for just one country:

db.bios.find( { Country: { $ne: "Country1" } } )