
- 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
Get MongoDB documents that contains specific attributes in array
For this, you can use $and along with dot(.) notation. Let us first create a collection with documents −
>db.demo2.insertOne({"StudentInformation":[{"StudentName":"John","StudentAge":21},{"StudentName":"Mike","StudentAge":22}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e08b56e25ddae1f53b62219") } >db.demo2.insertOne({"StudentInformation":[{"StudentName":"Carol","StudentAge":19},{"StudentName":"Bob","StudentAge":18}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e08b58625ddae1f53b6221a") }
Following is the query to display all documents from a collection with the help of find() method −
> db.demo2.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5e08b56e25ddae1f53b62219"), "StudentInformation" : [ { "StudentName" : "John", "StudentAge" : 21 }, { "StudentName" : "Mike", "StudentAge" : 22 } ] } { "_id" : ObjectId("5e08b58625ddae1f53b6221a"), "StudentInformation" : [ { "StudentName" : "Carol", "StudentAge" : 19 }, { "StudentName" : "Bob", "StudentAge" : 18 } ] }
Following is the query to get documents that contains specific attributes in array−
>db.demo2.find({$and:[{"StudentInformation.StudentName":"Carol"},{"StudentInformation.StudentName":"Bob"}]});
This will produce the following output −
{ "_id" : ObjectId("5e08b58625ddae1f53b6221a"), "StudentInformation" : [ { "StudentName" : "Carol", "StudentAge" : 19 }, { "StudentName" : "Bob", "StudentAge" : 18 } ] }
- Related Articles
- Find MongoDB documents that contains specific field?
- Find documents that contains specific field in MongoDB?
- MongoDB query to get only specific fields in nested array documents?
- Get count of array elements from a specific field in MongoDB documents?
- MongoDB query to find documents whose array contains a string that is a substring of a specific word
- Find document with array that contains a specific value in MongoDB
- Fetch specific multiple documents in MongoDB
- Find MongoDB documents where all objects in array have specific value?
- Find all documents that have two specific id's in an array of objects in MongoDB?
- Get specific elements from embedded array in MongoDB?
- MongoDB query to convert an array to a map of documents with n attributes?
- Find MongoDB documents where elements of an array have a specific value?
- Querying on an array of objects for specific nested documents with MongoDB?
- How to get items with a specific value from documents using MongoDB shell?
- MongoDB query to get specific list of names from documents where the value of a field is an array

Advertisements