
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
MongoDB Query to Implement NOR Query
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 }
Advertisements