
- 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
Select MongoDB documents where a field either does not exist, is null, or is false?
You can use $in operator for this. Let us first create a collection with a document. The query to create a collection with a document is as follows −
> db.selectMongoDBDocumentsWithSomeCondition.insertOne({"StudentId":1,"StudentName":"Larry"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9010215705caea966c557f") } > db.selectMongoDBDocumentsWithSomeCondition.insertOne({"StudentId":2,"StudentName":"Mike","hasAgeGreaterThanOrEqualTo18":true}); { "acknowledged" : true, "insertedId" : ObjectId("5c90106a5705caea966c5580") } > db.selectMongoDBDocumentsWithSomeCondition.insertOne({"StudentId":3,"StudentName":"Carol","hasAgeGreaterThanOrEqualTo18":false}); { "acknowledged" : true, "insertedId" : ObjectId("5c9010795705caea966c5581") } > db.selectMongoDBDocumentsWithSomeCondition.insertOne({"StudentId":4,"StudentName":"Sam","hasAgeGreaterThanOrEqualTo18":null}); { "acknowledged" : true, "insertedId" : ObjectId("5c9010865705caea966c5582") } > db.selectMongoDBDocumentsWithSomeCondition.insertOne({"StudentId":5,"StudentName":"David","hasAgeGreaterThanOrEqualTo18":false}); { "acknowledged" : true, "insertedId" : ObjectId("5c9010945705caea966c5583") } > db.selectMongoDBDocumentsWithSomeCondition.insertOne({"StudentId":6,"StudentName":"Chris","hasAgeGreaterThanOrEqualTo18":null}); { "acknowledged" : true, "insertedId" : ObjectId("5c9010a45705caea966c5584") } > db.selectMongoDBDocumentsWithSomeCondition.insertOne({"StudentId":7,"StudentName":"Robert","hasAgeGreaterThanOrEqualTo18":true}); { "acknowledged" : true, "insertedId" : ObjectId("5c9010b05705caea966c5585") }
Display all documents from a collection with the help of find() method. The query is as follows −
> db.selectMongoDBDocumentsWithSomeCondition.find().pretty();
The following is the output &minus
{ "_id" : ObjectId("5c9010215705caea966c557f"), "StudentId" : 1, "StudentName" : "Larry" } { "_id" : ObjectId("5c90106a5705caea966c5580"), "StudentId" : 2, "StudentName" : "Mike", "hasAgeGreaterThanOrEqualTo18" : true } { "_id" : ObjectId("5c9010795705caea966c5581"), "StudentId" : 3, "StudentName" : "Carol", "hasAgeGreaterThanOrEqualTo18" : false } { "_id" : ObjectId("5c9010865705caea966c5582"), "StudentId" : 4, "StudentName" : "Sam", "hasAgeGreaterThanOrEqualTo18" : null } { "_id" : ObjectId("5c9010945705caea966c5583"), "StudentId" : 5, "StudentName" : "David", "hasAgeGreaterThanOrEqualTo18" : false } { "_id" : ObjectId("5c9010a45705caea966c5584"), "StudentId" : 6, "StudentName" : "Chris", "hasAgeGreaterThanOrEqualTo18" : null } { "_id" : ObjectId("5c9010b05705caea966c5585"), "StudentId" : 7, "StudentName" : "Robert", "hasAgeGreaterThanOrEqualTo18" : true }
Here is the query to select MongoDB documents where a field either does not exist, is null, or is false −
> db.selectMongoDBDocumentsWithSomeCondition.find({ "hasAgeGreaterThanOrEqualTo18": {$in: [false,null]}}).pretty();
The following is the output −
{ "_id" : ObjectId("5c9010215705caea966c557f"), "StudentId" : 1, "StudentName" : "Larry" } { "_id" : ObjectId("5c9010795705caea966c5581"), "StudentId" : 3, "StudentName" : "Carol", "hasAgeGreaterThanOrEqualTo18" : false } { "_id" : ObjectId("5c9010865705caea966c5582"), "StudentId" : 4, "StudentName" : "Sam", "hasAgeGreaterThanOrEqualTo18" : null } { "_id" : ObjectId("5c9010945705caea966c5583"), "StudentId" : 5, "StudentName" : "David", "hasAgeGreaterThanOrEqualTo18" : false } { "_id" : ObjectId("5c9010a45705caea966c5584"), "StudentId" : 6, "StudentName" : "Chris", "hasAgeGreaterThanOrEqualTo18" : null }
- Related Articles
- How to query for records where field is null or not set in MongoDB?
- Check whether field exist in MongoDB or not?
- Select from table where value does not exist with MySQL?
- MongoDB query to select one field if the other is null and the first field if both are not null?
- Select documents grouped by field in MongoDB?
- MongoDB query to select one field if the other is null?
- Find MongoDB records where array field is not empty?
- Finding documents in MongoDB collection where a field is equal to given integer value?
- How to select MongoDB document that does not consist a specific field?
- MongoDB query to determine if a specific value does not exist?
- Insert records in MongoDB collection if it does not exist?
- Ignore null values in MongoDB documents
- MongoDB query to get specific list of names from documents where the value of a field is an array
- Sum MongoDB Sub-documents field?
- Check whether a field is empty or null in MySQL?

Advertisements