
- 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
Fetch records from a subdocument array wherein id begins from 234 in MongoDB
To fetch records from a subdocument array, use $unwind along with $push. For ids beginning from 234, use regex in MongoDB.
Let us create a collection with documents −
> db.demo556.insertOne( ... { ... _id:101, ... details:[ ... { ... id:"234336", ... Name:"Chris" ... }, ... { ... id:"123456", ... Name:"Bob" ... }, ... { ... id:"234987", ... Name:"Carol" ... }, ... { ... id:"989768", ... Name:"David" ... }, ... { ... id:"234888", ... Name:"Sam" ... }, ... { ... id:"847656", ... Name:"John" ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : 101 }
Display all documents from a collection with the help of find() method −
> db.demo556.find();
This will produce the following output −
{ "_id" : 101, "details" : [ { "id" : "234336", "Name" : "Chris" }, { "id" : "123456", "Name" : "Bob" }, { "id" : "234987", "Name" : "Carol" }, { "id" : "989768", "Name" : "David" }, { "id" : "234888", "Name" : "Sam" }, { "id" : "847656", "Name" : "John" } ] }
Following is the query to fetch records from a subdocument array −
> db.demo556.aggregate({ ... $match: { ... "_id": 101 ... } ... }, { ... $unwind: "$details" ... }, { ... $match: { ... "details.id": { ... $regex: /^234/ ... } ... } ... }, { ... $group: { ... _id: "$_id", ... "Detail": { ... $push: "$details" ... } ... } ... }).pretty();
This will produce the following output −
{ "_id" : 101, "Detail" : [ { "id" : "234336", "Name" : "Chris" }, { "id" : "234987", "Name" : "Carol" }, { "id" : "234888", "Name" : "Sam" } ] }
- Related Articles
- Fetch similar ID records from two tables in MySQL
- MongoDB query to remove subdocument from document?
- MySQL query to fetch records wherein timestamp is before 15+ days?
- Fetch unique records from table in SAP ABAP
- Match ID and fetch documents with $eq in MongoDB in case of array?
- Fetch records from comma separated values using MySQL IN()?
- MySQL query to fetch specific records matched from an array (comma separated values)
- How to filter array in subdocument with MongoDB?
- MySQL query to fetch records from a range of months?
- MySQL query to select records beginning from a specific id
- Fetch records in MongoDB on querying its subset
- Using aggregation pipeline to fetch records in MongoDB
- Exclude some ID records from a list and display rest in MySQL
- Fetch records from interval of past 3 days from current date in MySQL and add the corresponding records
- How to fetch the newly added records from a MySQL table?

Advertisements