
- 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
MongoDB inverse of query to return all items except specific documents?
To get documents except some specific documents, use $nor along with $and. Let us first create a collection with documents −
> db.demo1.insertOne({"StudentName":"Chris","StudentMarks":38}); { "acknowledged" : true, "insertedId" : ObjectId("5e08a4f025ddae1f53b62216") } > db.demo1.insertOne({"StudentName":"David","StudentMarks":78}); { "acknowledged" : true, "insertedId" : ObjectId("5e08a4f725ddae1f53b62217") } > db.demo1.insertOne({"StudentName":"Mike","StudentMarks":96}); { "acknowledged" : true, "insertedId" : ObjectId("5e08a4fd25ddae1f53b62218") }
Following is the query to display all documents from a collection with the help of find() method −
> db.demo1.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5e08a4f025ddae1f53b62216"), "StudentName" : "Chris", "StudentMarks" : 38 } { "_id" : ObjectId("5e08a4f725ddae1f53b62217"), "StudentName" : "David", "StudentMarks" : 78 } { "_id" : ObjectId("5e08a4fd25ddae1f53b62218"), "StudentName" : "Mike", "StudentMarks" : 96 }
Here is the query to get inverse of query −
> db.demo1.find({$nor:[{$and:[{'StudentName':'David'},{'StudentMarks':78}]}]});
This will produce the following output. The result displays Student records with marks except 78 −
{ "_id" : ObjectId("5e08a4f025ddae1f53b62216"), "StudentName" : "Chris", "StudentMarks" : 38 } { "_id" : ObjectId("5e08a4fd25ddae1f53b62218"), "StudentName" : "Mike", "StudentMarks" : 96 }
- Related Articles
- MongoDB query to implement nor query to fetch documents except a specific document
- MongoDB query to update all documents matching specific IDs
- MongoDB query to get a specific number of items
- How to query all items in MongoDB?
- MongoDB query to find documents with specific FirstName and LastName
- MongoDB query to display documents with a specific name irrespective of case
- MongoDB query to display all the fields value, except _id
- MongoDB query to get only specific fields in nested array documents?
- MongoDB query to return specific fields from an array?
- MongoDB query to add up the values of a specific field in documents
- How to get items with a specific value from documents using MongoDB shell?
- MySQL query to return all items in a single row
- MongoDB query to skip documents
- How to remove all documents from a collection except a single document in MongoDB?
- Get the size of all the documents in a MongoDB query?

Advertisements