To find records that does not match a condition, use $ne. Let us create a collection with documents −
> db.demo148.insertOne({"Message":"Hello"}); { "acknowledged" : true, "insertedId" : ObjectId("5e32fb37fdf09dd6d08539c0") } > db.demo148.insertOne({"Message":"Good"}); { "acknowledged" : true, "insertedId" : ObjectId("5e32fb3efdf09dd6d08539c1") } > db.demo148.insertOne({"Message":"Bye"}); { "acknowledged" : true, "insertedId" : ObjectId("5e32fb42fdf09dd6d08539c2") }
Display all documents from a collection with the help of find() method −
> db.demo148.find();
This will produce the following output −
{ "_id" : ObjectId("5e32fb37fdf09dd6d08539c0"), "Message" : "Hello" } { "_id" : ObjectId("5e32fb3efdf09dd6d08539c1"), "Message" : "Good" } { "_id" : ObjectId("5e32fb42fdf09dd6d08539c2"), "Message" : "Bye" }
Following is the query to find records in MongoDB that does not match a condition −
> db.demo148.find({'Message' : {'$ne' : 'Good'}}).toArray(function(err, output) ... { ... console.log(output); ... });
This will produce the following output −
[ { "_id" : ObjectId("5e32fb37fdf09dd6d08539c0"), "Message" : "Hello" }, { "_id" : ObjectId("5e32fb42fdf09dd6d08539c2"), "Message" : "Bye" } ]