
- 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
Querying null value in MongoDB?
To query null value in MongoDB, use $ne operator. Let us first create a collection with documents −
> db.queryingNullDemo.insertOne( ... { ... "StudentName":"Larry", ... "StudentDetails": ... { ... "StudentAge":21, ... "StudentSubject":"MongoDB" ... } ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5cd00bec588d4a6447b2e05f") } > db.queryingNullDemo.insertOne( ... { ... "StudentName":"Sam", ... "StudentDetails": ... { ... "StudentAge":23, ... "StudentSubject":null ... } ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5cd00c00588d4a6447b2e060") }
Following is the query to display all documents from a collection with the help of find() method −
> db.queryingNullDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cd00bec588d4a6447b2e05f"), "StudentName" : "Larry", "StudentDetails" : { "StudentAge" : 21, "StudentSubject" : "MongoDB" } } { "_id" : ObjectId("5cd00c00588d4a6447b2e060"), "StudentName" : "Sam", "StudentDetails" : { "StudentAge" : 23, "StudentSubject" : null } }
Following is how you can query for null −
> db.queryingNullDemo.find({'StudentDetails.StudentSubject': {$ne: null}});
This will produce the following output −
{ "_id" : ObjectId("5cd00bec588d4a6447b2e05f"), "StudentName" : "Larry", "StudentDetails" : { "StudentAge" : 21, "StudentSubject" : "MongoDB" } }
- Related Articles
- Querying with MongoDB subelement?
- Querying internal array size in MongoDB?
- Querying array elements with MongoDB?
- How to improve querying field in MongoDB?
- Querying an array of arrays in MongoDB?
- Is MongoDB able to index a null value?
- Fetch records in MongoDB on querying its subset
- Querying object's field array values in MongoDB?
- Calculate the average value in a MongoDB document grouping by null?
- Get at least one match in list querying with MongoDB?
- Querying array of Embedded Documents in MongoDB based on Range?
- Querying from part of object in an array with MongoDB
- How do I check whether a field contains null value in MongoDB?
- Check for null in MongoDB?
- Native Querying MongoDB inside array and get the count

Advertisements