
- 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 query to implement nor query to fetch documents except a specific document
To fetch documents except a specific document, set the document to be missed using the $nor in MongoDB. Let us create a collection with documents −
> db.demo100.insertOne({"Name":"Chris","Age":21}); { "acknowledged" : true, "insertedId" : ObjectId("5e2d9624b8903cdd865577c0") } > db.demo100.insertOne({"Name":"David","Age":23}); { "acknowledged" : true, "insertedId" : ObjectId("5e2d962cb8903cdd865577c1") } > db.demo100.insertOne({"Name":"Bob","Age":19}); { "acknowledged" : true, "insertedId" : ObjectId("5e2d9634b8903cdd865577c2") }
Display all documents from a collection with the help of find() method −
> db.demo100.find();
This will produce the following output −
{ "_id" : ObjectId("5e2d9624b8903cdd865577c0"), "Name" : "Chris", "Age" : 21 } { "_id" : ObjectId("5e2d962cb8903cdd865577c1"), "Name" : "David", "Age" : 23 } { "_id" : ObjectId("5e2d9634b8903cdd865577c2"), "Name" : "Bob", "Age" : 19 }
Following is the query to implement NOR query in MongoDB −
> db.demo100.find({ $nor: [ { Name:"Chris" }, { Name: { $exists: false } }, { Age: 21 }, { Age: { $exists: false } } ] } );
This will produce the following output −
{ "_id" : ObjectId("5e2d962cb8903cdd865577c1"), "Name" : "David", "Age" : 23 } { "_id" : ObjectId("5e2d9634b8903cdd865577c2"), "Name" : "Bob", "Age" : 19 }
- Related Articles
- MongoDB query to fetch a specific document rom documents with field value set using NumberInt()?
- MongoDB inverse of query to return all items except specific documents?
- MongoDB query to remove a specific document
- MongoDB query to update a specific document from a collection
- MongoDB query to pull a specific value from a document
- Query an array in MongoDB to fetch a specific value
- MongoDB query to update all documents matching specific IDs
- MongoDB query to find documents with specific FirstName and LastName
- Fetch multiple documents in MongoDB query with OR condition?
- MongoDB query to skip documents
- MongoDB query to fetch a document that does not have a particular field?
- MongoDB query to display documents with a specific name irrespective of case
- MongoDB query to get only specific fields in nested array documents?
- MongoDB query to add multiple documents
- MongoDB query to group duplicate documents

Advertisements