
- 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
Search for documents matching first item in an array with MongoDB?
Let us first create a collection with documents −
> db.matchingFirstItemInTheArrayDemo.insertOne( { "ClientDetails": [ { "ClientName": "Larry", "ClientAge":28 } ] } ); { "acknowledged" : true, "insertedId" : ObjectId("5cd7a5d26d78f205348bc636") } > db.matchingFirstItemInTheArrayDemo.insertOne( { "ClientDetails": [ { "ClientName": "Chris", "ClientAge":56, } ] } ); { "acknowledged" : true, "insertedId" : ObjectId("5cd7a5f56d78f205348bc637") } > db.matchingFirstItemInTheArrayDemo.insertOne( { "ClientDetails": [ { "ClientName": "Robert", "ClientAge":46, } ] } ); { "acknowledged" : true, "insertedId" : ObjectId("5cd7a6076d78f205348bc638") }
Following is the query to display all documents from a collection with the help of find() method −
> db.matchingFirstItemInTheArrayDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cd7a5d26d78f205348bc636"), "ClientDetails" : [ { "ClientName" : "Larry", "ClientAge" : 28 } ] } { "_id" : ObjectId("5cd7a5f56d78f205348bc637"), "ClientDetails" : [ { "ClientName" : "Chris", "ClientAge" : 56 } ] } { "_id" : ObjectId("5cd7a6076d78f205348bc638"), "ClientDetails" : [ { "ClientName" : "Robert", "ClientAge" : 46 } ] }
Following is the query to search for documents matching first item in array −
> db.matchingFirstItemInTheArrayDemo.find({"ClientDetails.0.ClientName":"Chris"});
This will produce the following output −
{ "_id" : ObjectId("5cd7a5f56d78f205348bc637"), "ClientDetails" : [ { "ClientName" : "Chris", "ClientAge" : 56 } ] }
- Related Articles
- MongoDB query to find matching documents given an array with values?
- Getting only the first item for an array property in MongoDB?
- MongoDB query for documents matching array, irrespective of elements order
- Search for multiple documents in MongoDB?
- How to search documents through an integer array in MongoDB?
- Querying on an array of objects for specific nested documents with MongoDB?
- Search for documents with similar arrays in MongoDB and order by similarity value
- MongoDB exact array matching
- Search an array of hashes in MongoDB?
- Golang Program to search an item into the array using interpolation search
- MongoDB query to update all documents matching specific IDs
- How to search for an item in a Lua List?
- MongoDB aggregate to convert multiple documents into single document with an array?
- Find in MongoDB documents with filled nested array and reshape the documents result
- Check for existing documents/embedded documents in MongoDB

Advertisements