
- MongoDB Tutorial
- MongoDB - Home
- MongoDB - Overview
- MongoDB - Advantages
- MongoDB - Environment
- MongoDB - Data Modeling
- MongoDB - Create Database
- MongoDB - Drop Database
- MongoDB - Create Collection
- MongoDB - Drop Collection
- MongoDB - Data Types
- MongoDB - Insert Document
- MongoDB - Query Document
- MongoDB - Update Document
- MongoDB - Delete Document
- MongoDB - Projection
- MongoDB - Limiting Records
- MongoDB - Sorting Records
- MongoDB - Indexing
- MongoDB - Aggregation
- MongoDB - Replication
- MongoDB - Sharding
- MongoDB - Create Backup
- MongoDB - Deployment
- MongoDB - Java
- MongoDB - PHP
- Advanced MongoDB
- MongoDB - Relationships
- MongoDB - Database References
- MongoDB - Covered Queries
- MongoDB - Analyzing Queries
- MongoDB - Atomic Operations
- MongoDB - Advanced Indexing
- MongoDB - Indexing Limitations
- MongoDB - ObjectId
- MongoDB - Map Reduce
- MongoDB - Text Search
- MongoDB - Regular Expression
- Working with Rockmongo
- MongoDB - GridFS
- MongoDB - Capped Collections
- Auto-Increment Sequence
- MongoDB Useful Resources
- MongoDB - Questions and Answers
- MongoDB - Quick Guide
- MongoDB - Useful Resources
- MongoDB - Discussion
How to query with nand operator in MongoDB?
The $not operator won’t invert a complex expression. Therefore, use $and or $or with $ne operator.
Let us create a collection with documents −
> db.demo266.insertOne({"active1":true,"active2":false}); { "acknowledged" : true, "insertedId" : ObjectId("5e480f4b1627c0c63e7dbaa7") } > db.demo266.insertOne({"active1":true,"active2":true}); { "acknowledged" : true, "insertedId" : ObjectId("5e480f501627c0c63e7dbaa8") } > db.demo266.insertOne({"active1":false,"active2":false}); { "acknowledged" : true, "insertedId" : ObjectId("5e480f561627c0c63e7dbaa9") } > db.demo266.insertOne({"active1":false,"active2":true}); { "acknowledged" : true, "insertedId" : ObjectId("5e480f701627c0c63e7dbaaa") }
Display all documents from a collection with the help of find() method −
> db.demo266.find();
This will produce the following output −
{ "_id" : ObjectId("5e480f4b1627c0c63e7dbaa7"), "active1" : true, "active2" : false } { "_id" : ObjectId("5e480f501627c0c63e7dbaa8"), "active1" : true, "active2" : true } { "_id" : ObjectId("5e480f561627c0c63e7dbaa9"), "active1" : false, "active2" : false } { "_id" : ObjectId("5e480f701627c0c63e7dbaaa"), "active1" : false, "active2" : true }
Following is the query to use $or with $ne operator −
> db.demo266.find({$or:[{"active1":{"$ne":true}},{"active2":{"$ne":true}}]});
This will produce the following output −
{ "_id" : ObjectId("5e480f4b1627c0c63e7dbaa7"), "active1" : true, "active2" : false } { "_id" : ObjectId("5e480f561627c0c63e7dbaa9"), "active1" : false, "active2" : false } { "_id" : ObjectId("5e480f701627c0c63e7dbaaa"), "active1" : false, "active2" : true }
- Related Articles
- How to query MongoDB using the $ne operator?
- MongoDB query to implement OR operator in find()
- How to query MongoDB with “like”?
- How to work with variables in MongoDB query
- How to query MongoDB with a LIMIT?
- How to work Date query with ISODate in MongoDB?
- How to query MongoDB a value with $lte and $in?
- How to update many documents with one query in MongoDB?
- MongoDB to fetch documents with $or Operator
- MongoDB pull with positional operator?
- Using positional operator with hierarchies in MongoDB?
- How to implement MongoDB $or operator
- How to query objects with the longest time period in MongoDB?
- Can I query on a MongoDB index if my query contains the $or operator?
- MongoDB query with $all in array

Advertisements