- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
How to select MongoDB document that does not consist a specific field?
Check for a specific field using MongoDB $exists. If that field does not exist in a document, then you need to display the same document with find().
Let us create a collection with documents −
> db.demo612.insertOne({id:1,"Info":[{Name:"Chris",Age:21},{Name:"David"}]});{ "acknowledged" : true, "insertedId" : ObjectId("5e987372f6b89257f5584d87") }
Display all documents from a collection with the help of find() method −
> db.demo612.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5e987372f6b89257f5584d87"), "id" : 1, "Info" : [ { "Name" : "Chris", "Age" : 21 }, { "Name" : "David" } ] }
Following is the query to fetch a MongoDB document that does not consist of a specific field −
> db.demo612.aggregate({$unwind: "$Info"}, ... {$match: {"Info.Age":{$exists: false}}}, ... {$project: {"Info.Name": 1}})
This will produce the following output −
{ "_id" : ObjectId("5e987372f6b89257f5584d87"), "Info" : { "Name" : "David" } }
- Related Articles
- MongoDB query to fetch a document that does not have a particular field?
- MongoDB query select and display only a specific field from the document?
- How to add a field with specific datatype (list, object) in an existing MongoDB document?
- How to select objects where an array contains only a specific field in MongoDB?
- Find the document by field name with a specific value in MongoDB?
- Select MongoDB documents where a field either does not exist, is null, or is false?
- MongoDB query to remove a specific document
- Find MongoDB documents that contains specific field?
- How to remove a field completely from a MongoDB document?
- How to select a specific subdocument in MongoDB?
- How to select a single field in MongoDB?
- MongoDB query to fetch a specific document rom documents with field value set using NumberInt()?
- Find document with array that contains a specific value in MongoDB
- Find documents that contains specific field in MongoDB?
- MongoDB query to determine if a specific value does not exist?

Advertisements