
- 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
Query Array for 'true' value at index n in MongoDB?
You can use dot(.) notation for this. Let us first create a collection with documents −
>db.containsTrueValueDemo.insertOne({"IsMarried":[true,false,true,true,true,true,false,true,false,false,true]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd5039c2cba06f46efe9ef5") }
Following is the query to display all documents from a collection with the help of find() method −
> db.containsTrueValueDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cd5039c2cba06f46efe9ef5"), "IsMarried" : [ true, false, true, true, true, true, false, true, false, false, true ] }
Query Array for 'true' value at index n -
> db.containsTrueValueDemo.find({'IsMarried.2' : true});
This will produce the following output −
{ "_id" : ObjectId("5cd5039c2cba06f46efe9ef5"), "IsMarried" : [ true, false, true, true, true, true, false, true, false, false, true ] }
Query to Array for 'true' value at index n −
> db.containsTrueValueDemo.find({$and : [ {'IsMarried.0' : true}, {'IsMarried.2' : true} ] } );
This will produce the following output −
{ "_id" : ObjectId("5cd5039c2cba06f46efe9ef5"), "IsMarried" : [ true, false, true, true, true, true, false, true, false, false, true ] }
- Related Articles
- MongoDB query to update array object in index N?
- Update object at specific Array Index in MongoDB?
- MongoDB query for array concatenation?
- MongoDB Query for boolean field as “not true”
- MongoDB query check if value in array property?
- MongoDB query to replace value in an array?
- MongoDB query on nth element (variable index) of subdocument array
- Insert to specific index for MongoDB array?
- MongoDB query for Partial Object in an array
- MongoDB query for documents whose array elements does not have a specific value
- MongoDB query to test if a value is in array?
- Query an array in MongoDB to fetch a specific value
- MongoDB query for capped sub-collection in an array
- Which MongoDB query finds same value multiple times in an array?
- MongoDB query to find value in array with multiple criteria (range)

Advertisements