
- 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
MongoDB query to find on the basis of true or false values
To find on the basis of true or false values, use $exists in find(). You would also need dot notation for the same task.
Let us first create a collection with documents −
> db.demo367.insertOne( ... { "Id" : "102", ... "details" : [ { "Name" : "David"}, ... { "Age" : 23, "CountryName" : "UK"} ], ... "isMarried" : false } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e57e0b62ae06a1609a00ae8") } > db.demo367.insertOne( ... { "Id" : "101", ... "details" : [ { "Name" : "Chris", "Subject" : [ "MySQL" ] }, ... { "Age" : 21, "CountryName" : "US"} ], ... "isMarried" : true } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e57e0be2ae06a1609a00ae9") }
Display all documents from a collection with the help of find() method −
> db.demo367.find();
This will produce the following output −
{ "_id" : ObjectId("5e57e0b62ae06a1609a00ae8"), "Id" : "102", "details" : [ { "Name" : "David" }, { "Age" : 23, "CountryName" : "UK" } ], "isMarried" : false } { "_id" : ObjectId("5e57e0be2ae06a1609a00ae9"), "Id" : "101", "details" : [ { "Name" : "Chris", "Subject" : [ "MySQL" ] }, { "Age" : 21, "CountryName" : "US" } ], "isMarried" : true }
Following is the query to find on the basis of true false values by checking with $exists −
> db.demo367.find({"details.Subject": { $exists: false}});
This will produce the following output −
{ "_id" : ObjectId("5e57e0b62ae06a1609a00ae8"), "Id" : "102", "details" : [ { "Name" : "David" }, { "Age" : 23, "CountryName" : "UK" } ], "isMarried" : false }
- Related Articles
- MongoDB query to count records on the basis of matching criteria
- State True or False-
- MySQL query to display records on the basis of conditions IS NULL OR !=1;?
- How to use or operator in MongoDB to fetch records on the basis of existence?
- Why does MySQL evaluate “TRUE or TRUE and FALSE” to true?
- MongoDB query to exclude both the fields with FALSE
- Indicate the following, is true or false.
- MySQL db query to fetch records from comma separate values on the basis of a specific value
- MongoDB query to implement OR operator in find()
- Get only the FALSE value with MongoDB query
- Find average on the basis of corresponding duplicate VARCHAR values in MySQL
- $-6$ is the additive inverse of 6. True or False?
- MySQL query to select three highest values and sort alphabetically on the basis of corresponding column with name
- How to remove document on the basis of _id in MongoDB?
- MySQL query to ORDER BY records on the basis of modulus result

Advertisements