
- 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
What is the syntax for boolean values in MongoDB?
You can use $eq as well as $ne operator for boolean values. Let us first create a collection with documents −
> db.booleanQueryDemo.insertOne({"UserName":"John","UserAge":23,"isMarried":true}); { "acknowledged" : true, "insertedId" : ObjectId("5cc815c08f9e6ff3eb0ce44a") } > db.booleanQueryDemo.insertOne({"UserName":"Chris","UserAge":21,"isMarried":false}); { "acknowledged" : true, "insertedId" : ObjectId("5cc815d08f9e6ff3eb0ce44b") } > db.booleanQueryDemo.insertOne({"UserName":"Robert","UserAge":24,"isMarried":false}); { "acknowledged" : true, "insertedId" : ObjectId("5cc815dc8f9e6ff3eb0ce44c") } > db.booleanQueryDemo.insertOne({"UserName":"David","UserAge":26,"isMarried":true}); { "acknowledged" : true, "insertedId" : ObjectId("5cc815ed8f9e6ff3eb0ce44d") }
Following is the query to display all documents from a collection with the help of find() method −
> db.booleanQueryDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cc815c08f9e6ff3eb0ce44a"), "UserName" : "John", "UserAge" : 23, "isMarried" : true } { "_id" : ObjectId("5cc815d08f9e6ff3eb0ce44b"), "UserName" : "Chris", "UserAge" : 21, "isMarried" : false } { "_id" : ObjectId("5cc815dc8f9e6ff3eb0ce44c"), "UserName" : "Robert", "UserAge" : 24, "isMarried" : false } { "_id" : ObjectId("5cc815ed8f9e6ff3eb0ce44d"), "UserName" : "David", "UserAge" : 26, "isMarried" : true }
Case 1 − When the value is equal to false.
Following is the query for boolean values −
> db.booleanQueryDemo.find({isMarried: {$eq: false}}).pretty();
This will produce the following output −
{ "_id" : ObjectId("5cc815d08f9e6ff3eb0ce44b"), "UserName" : "Chris", "UserAge" : 21, "isMarried" : false } { "_id" : ObjectId("5cc815dc8f9e6ff3eb0ce44c"), "UserName" : "Robert", "UserAge" : 24, "isMarried" : false }
Case 2 − When the value is equal to true.
Following is the query −
> db.booleanQueryDemo.find({isMarried: {$ne: false}}).pretty();
This will produce the following output −
{ "_id" : ObjectId("5cc815c08f9e6ff3eb0ce44a"), "UserName" : "John", "UserAge" : 23, "isMarried" : true } { "_id" : ObjectId("5cc815ed8f9e6ff3eb0ce44d"), "UserName" : "David", "UserAge" : 26, "isMarried" : true }
- Related Articles
- What is the syntax for lambda expressions in Java?
- What is the syntax for leading bang! in JavaScript function?
- What is the correct syntax for NOT LIKE in MySQL?
- What is the type specifier for boolean in C++?
- What is the syntax for defining the data structure in C++?
- MongoDB Query for boolean field as “not true”
- What is basic syntax of Python for Loops?
- What is the syntax for input parameters (variables) in a MySQL query?
- Boolean Values in Python
- What is Syntax Tree?
- MongoDB syntax for updating an object inside an array within a document?
- What is the Syntax Directed Translation?\n
- What is the syntax to define enums in javascript?
- MongoDB transaction & indexes for duplicate values
- Search multiple fields for multiple values in MongoDB?

Advertisements