- 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
MongoDB query to fetch a document that does not have a particular field?
To check for existence, use $exists. Let us create a collection with documents −
> db.demo234.insertOne({"FirstName":"Chris","LastName":"Brown","Age":24}); { "acknowledged" : true, "insertedId" : ObjectId("5e418a50f4cebbeaebec5148") } > db.demo234.insertOne({"FirstName":"David","LastName":"Miller"}); { "acknowledged" : true, "insertedId" : ObjectId("5e418a5ff4cebbeaebec5149") } > db.demo234.insertOne({"FirstName":"John","LastName":"Smith",Age:34}); { "acknowledged" : true, "insertedId" : ObjectId("5e418a70f4cebbeaebec514a") }
Display all documents from a collection with the help of find() method −
> db.demo234.find();
This will produce the following output −
{ "_id" : ObjectId("5e418a50f4cebbeaebec5148"), "FirstName" : "Chris", "LastName" : "Brown", "Age" : 24 } { "_id" : ObjectId("5e418a5ff4cebbeaebec5149"), "FirstName" : "David", "LastName" : "Miller" } { "_id" : ObjectId("5e418a70f4cebbeaebec514a"), "FirstName" : "John", "LastName" : "Smith", "Age" : 34 }
Following is the query to check for existence and fetch document without “Age” field −
> var iterator = db.demo234.find({"Age":{$exists:false}},{"_id":0}); > iterator.forEach(function(d) ...{ ... printjson(d); ...})
This will produce the following output −
{ "FirstName" : "David", "LastName" : "Miller" }
- Related Articles
- How to select MongoDB document that does not consist a specific field?
- MongoDB query to fetch a specific document rom documents with field value set using NumberInt()?
- MongoDB query to implement nor query to fetch documents except a specific document
- Find documents with arrays not containing a document with a particular field value in MongoDB?
- Find items that do not have a certain field in MongoDB?
- Is it possible to use MongoDB to query for entries that have a particular value in a field in an object in an array?
- MySQL query to get a field value that does not contain empty spaces?
- MongoDB query for documents whose array elements does not have a specific value
- MongoDB query to exclude if id is equal to a document field array value
- MongoDB query select and display only a specific field from the document?
- MongoDB “$and” operator for subcollection to fetch a document?
- MongoDB query to fetch only the “Name” field based on roles?
- MongoDB query to remove a specific document
- How to delete particular data in a document in MongoDB?
- MongoDB query to determine if a specific value does not exist?

Advertisements