
- 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
Find document with array that contains a specific value in MongoDB
You can use find() method to find document with array that contains a specific value. The syntax is as follows:
db.yourCollectionName.find({"yourArrayFieldName":"yourValue"},.......N).pretty();
To understand the above syntax, let us create a collection with documents. The query to create a collection with documents is as follows:
>db.findSpecificValue.insertOne({"StudentId":1,"StudentName":"Larry","FavouriteSubject":["C","C++","Java"]}); { "acknowledged" : true, "insertedId" : ObjectId("5c6e8996140577d89182b8d0") } >db.findSpecificValue.insertOne({"StudentId":2,"StudentName":"Larry","FavouriteSubject":["MongoDB","MySQL","SQL Server"]}); { "acknowledged" : true, "insertedId" : ObjectId("5c6e89b1140577d89182b8d1") }
Display all documents from a collection with the help of find() method. The query is as follows:
> db.findSpecificValue.find().pretty();
The following is the output:
{ "_id" : ObjectId("5c6e8996140577d89182b8d0"), "StudentId" : 1, "StudentName" : "Larry", "FavouriteSubject" : [ "C", "C++", "Java" ] } { "_id" : ObjectId("5c6e89b1140577d89182b8d1"), "StudentId" : 2, "StudentName" : "Larry", "FavouriteSubject" : [ "MongoDB", "MySQL", "SQL Server" ] }
Here is the query to find document with array that contains a specific value i.e. “MongoDB” for FavouriteSubject here:
> db.findSpecificValue.find({"FavouriteSubject":"MongoDB"}).pretty();
The following is the output:
{ "_id" : ObjectId("5c6e89b1140577d89182b8d1"), "StudentId" : 2, "StudentName" : "Larry", "FavouriteSubject" : [ "MongoDB", "MySQL", "SQL Server" ] }
- Related Articles
- Find which MongoDB document contains a specific string?
- Find MongoDB document with array containing the maximum occurrence of a specific value
- Find the document by field name with a specific value in MongoDB?
- Fetch a specific document in MongoDB with array elements
- Get MongoDB documents that contains specific attributes in array
- Find MongoDB documents that contains specific field?
- Find documents that contains specific field in MongoDB?
- Find a strict document that contains only a specific field with a fixed length?\n
- Update only a specific value in a MongoDB document
- Update a specific MongoDB document in array with $set and positional $ operator?
- How to find specific array elements in MongoDB document with query and filter with range?
- Extract a MongoDB document with a specific string
- Find document that matches same array elements in MongoDB?
- MongoDB query to pull a specific value from a document
- MongoDB query to find documents whose array contains a string that is a substring of a specific word

Advertisements