
- 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
How to find a document by the non-existence of a field in MongoDB?
To find a document by the non-existence of a field in MongoDB, the syntax is as follows −
db.yourCollectionName.find({ "yourFieldName" : { "$exists" : false } }).pretty();
To understand the above syntax, let us create a collection with the document. The query to create a collection with a document is as follows −
> db.findDocumentNonExistenceFieldDemo.insertOne({"StudentName":"John","StudentAge":25}); { "acknowledged" : true, "insertedId" : ObjectId("5c8a5c629064dcd4a68b70e8") } > db.findDocumentNonExistenceFieldDemo.insertOne({"StudentName":"David","StudentAge":26,"StudentMathMarks":78}); { "acknowledged" : true, "insertedId" : ObjectId("5c8a5c809064dcd4a68b70e9") }
Display all documents from a collection with the help of find() method. The query is as follows −
> db.findDocumentNonExistenceFieldDemo.find().pretty();
The following is the output −
{ "_id" : ObjectId("5c8a5c629064dcd4a68b70e8"), "StudentName" : "John", "StudentAge" : 25 } { "_id" : ObjectId("5c8a5c809064dcd4a68b70e9"), "StudentName" : "David", "StudentAge" : 26, "StudentMathMarks" : 78 }
Here is the query to find a document by the non-existence of a field −
> db.findDocumentNonExistenceFieldDemo.find({ "StudentMathMarks" : { "$exists" : false } }).pretty();
The following is the output −
{ "_id" : ObjectId("5c8a5c629064dcd4a68b70e8"), "StudentName" : "John", "StudentAge" : 25 }
- Related Articles
- Find the document by field name with a specific value in MongoDB?
- How to sum every field in a sub document of MongoDB?
- How to remove a field completely from a MongoDB document?
- Find all the non-distinct values of a field in MongoDB?
- How to add an extra field in a sub document in MongoDB?
- Add new field to every document in a MongoDB collection?
- Increment a field in MongoDB document which is embedded?
- How to select MongoDB document that does not consist a specific field?
- How to find a certain element in the MongoDB embedded document?
- Check if value exists for a field in a MongoDB document?
- Add a field to an embedded document in an array in MongoDB?
- How to get distinct list of sub-document field values in MongoDB?
- Find documents with arrays not containing a document with a particular field value in MongoDB?
- How to find only a single document satisfying the criteria in MongoDB?
- How to delete a document in MongoDB?

Advertisements