
- 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 test if a value is in array?
To check for a specific value, use $in. Let us first create a collection with documents −
> db.testInArray.insertOne({"ListOfNumbers":[10,56,78,90,32]}); { "acknowledged" : true, "insertedId" : ObjectId("5e04d42df5e889d7a519950d") } > db.testInArray.insertOne({"ListOfNumbers":[56,78,91,100]}); { "acknowledged" : true, "insertedId" : ObjectId("5e04d588f5e889d7a519950e") }
Following is the query to display all documents from a collection with the help of find() method −
> db.testInArray.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5e04d42df5e889d7a519950d"), "ListOfNumbers" : [ 10, 56, 78, 90, 32 ] } { "_id" : ObjectId("5e04d588f5e889d7a519950e"), "ListOfNumbers" : [ 56, 78, 91, 100 ] }
Here is the query to test if a value is in array −
> db.testInArray.find({ ListOfNumbers:{ "$in" : [90]} });
This will produce the following output −
{ "_id" : ObjectId("5e04d42df5e889d7a519950d"), "ListOfNumbers" : [ 10, 56, 78, 90, 32 ] }
- Related Articles
- MongoDB query check if value in array property?
- MongoDB query to exclude if id is equal to a document field array value
- MongoDB query to replace value in an array?
- Query an array in MongoDB to fetch a specific value
- MongoDB query to find value in array with multiple criteria (range)
- MongoDB query to determine if a specific value does not exist?
- MongoDB query to match documents with array values greater than a specific value
- MongoDB Query to implement $in in array
- Which MongoDB query finds same value multiple times in an array?
- Write a MongoDB query to get nested value?
- MongoDB query to fetch array values
- MongoDB query to aggregate nested array
- MongoDB query to sort nested array?
- How to query MongoDB a value with $lte and $in?
- Query Array for 'true' value at index n in MongoDB?

Advertisements