
- 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 exclude both the fields with FALSE
Use $or operator along with $expr operator for this. Let us first create a collection with documents, wherein one of fields is isMarried having true of false value −
> db.orTwoFieldsDemo.insertOne({"isLiveInUS":true,"isMarried":false}); { "acknowledged" : true, "insertedId" : ObjectId("5cdfd86abf3115999ed5120d") } > db.orTwoFieldsDemo.insertOne({"isLiveInUS":true,"isMarried":true}); { "acknowledged" : true, "insertedId" : ObjectId("5cdfd876bf3115999ed5120e") } > db.orTwoFieldsDemo.insertOne({"isLiveInUS":false,"isMarried":false}); { "acknowledged" : true, "insertedId" : ObjectId("5cdfd87dbf3115999ed5120f") }
Following is the query to display all documents from a collection with the help of find() method −
> db.orTwoFieldsDemo.find();
This will produce the following output −
{ "_id" : ObjectId("5cdfd86abf3115999ed5120d"), "isLiveInUS" : true, "isMarried" : false } { "_id" : ObjectId("5cdfd876bf3115999ed5120e"), "isLiveInUS" : true, "isMarried" : true } { "_id" : ObjectId("5cdfd87dbf3115999ed5120f"), "isLiveInUS" : false, "isMarried" : false }
Following is the query to avoid both the FALSE values in two fields and display only fields with TRUE or one of the field with TRUE or another FALSE vice-versa −
> db.orTwoFieldsDemo.find({ $expr: { $or: [ "$isLiveInUS", "$isMarried" ] } });
This will produce the following output −
{ "_id" : ObjectId("5cdfd86abf3115999ed5120d"), "isLiveInUS" : true, "isMarried" : false } { "_id" : ObjectId("5cdfd876bf3115999ed5120e"), "isLiveInUS" : true, "isMarried" : true }
- Related Articles
- MongoDB collection query to exclude some fields in find()?
- Is it possible to exclude nested fields in MongoDB with a wildcard?
- MongoDB query with fields in the same document?
- Get only the FALSE value with MongoDB query
- MongoDB query to update selected fields
- MongoDB query to sum specific fields
- MongoDB query to concatenate values of array with other fields
- MongoDB query to update only certain fields?
- MongoDB query condition to compare two fields?
- How to exclude _id without including other fields using the aggregation framework in MongoDB?
- MongoDB query to check the existence of multiple fields
- MongoDB query to display all the fields value, except _id
- MongoDB query condition on comparing 2 fields?
- MongoDB query for fields in embedded document?
- MongoDB query to group several fields using aggregation framework?

Advertisements