Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Get only the FALSE value with MongoDB query
To get only the documents with FALSE values from a boolean field in MongoDB, use the find() method with a query condition that matches the false value directly or uses comparison operators.
Syntax
db.collection.find({ "fieldName": false });
// OR
db.collection.find({ "fieldName": { "$ne": true } });
Sample Data
Let us first create a collection with documents having an isEnable field with TRUE or FALSE values ?
db.translateDefinitionDemo.insertMany([
{ "_id": 10, "StudentName": "Larry", "isEnable": true },
{ "_id": 20, "StudentName": "Chris", "isEnable": false },
{ "_id": 30, "StudentName": "Robert", "isEnable": true },
{ "_id": 40, "StudentName": "Sam", "isEnable": false },
{ "_id": 50, "StudentName": "Mike", "isEnable": true }
]);
{
"acknowledged": true,
"insertedIds": { "0": 10, "1": 20, "2": 30, "3": 40, "4": 50 }
}
Display all documents to see the complete data ?
db.translateDefinitionDemo.find().pretty();
{ "_id": 10, "StudentName": "Larry", "isEnable": true }
{ "_id": 20, "StudentName": "Chris", "isEnable": false }
{ "_id": 30, "StudentName": "Robert", "isEnable": true }
{ "_id": 40, "StudentName": "Sam", "isEnable": false }
{ "_id": 50, "StudentName": "Mike", "isEnable": true }
Method 1: Direct FALSE Match
Query documents where isEnable is exactly false ?
db.translateDefinitionDemo.find({ "isEnable": false }).pretty();
{ "_id": 20, "StudentName": "Chris", "isEnable": false }
{ "_id": 40, "StudentName": "Sam", "isEnable": false }
Method 2: Using $ne Operator
Find documents where isEnable is not equal to true ?
db.translateDefinitionDemo.find({ "isEnable": { "$ne": true } }).pretty();
{ "_id": 20, "StudentName": "Chris", "isEnable": false }
{ "_id": 40, "StudentName": "Sam", "isEnable": false }
Key Points
-
Direct match (
{ "field": false }) is simpler and more efficient for exact boolean queries. - $ne operator also catches null values and missing fields, not just false values.
- Both methods return the same result when the field contains only true/false values.
Conclusion
Use { "fieldName": false } to get documents with FALSE boolean values. The $ne operator provides an alternative approach but may include documents with null or missing fields.
Advertisements
