![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find records in MongoDB that does NOT match a condition?
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" } ]
Advertisements