- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
MongoDB Aggregate JSON array field for the matching field of other collection?
For this, create two collections and add some document. After that, use $lookup for match. Let us create a collection with documents −
> db.demo101.insertOne( ... { "_id" : "1", "Details" : [ { "PId" : "200" }, { "PId" : "201" }, { "PId" : "201" } ] } ... ) { "acknowledged" : true, "insertedId" : "1" }
Display all documents from a collection with the help of find() method −
> db.demo101.find();
This will produce the following output −
{ "_id" : "1", "Details" : [ { "PId" : "200" }, { "PId" : "201" }, { "PId" : "201" } ] }
Following is the query to create second collection with some documents −
> db.demo102.insertOne( ... { "_id" : "201", "CustEmailId" : "Carol@gmail.com" } ... ); { "acknowledged" : true, "insertedId" : "201" }
Display all documents from a collection with the help of find() method −
> db.demo102.find();
This will produce the following output −
{ "_id" : "200", "CustEmailId" : "John@gmail.com" } { "_id" : "201", "CustEmailId" : "Carol@gmail.com" }
Following is the query to aggregate JSON array field for the matching field of other collection −
> db.demo101.aggregate( ... [ ... {$unwind:"$Details"}, ... {$lookup : {from : "demo102", "localField":"Details.PId", "foreignField":"_id", as :"out"}}, ... {$project : {"_id":1, "Details.PId":{$arrayElemAt:["$out.CustEmailId",0]}}}, ... {$group:{_id:"$_id", Details : {$push : "$Details"}}} ... ] ... ).pretty()
This will produce the following output −
{ "_id" : "1", "Details" : [ { "PId" : "John@gmail.com" }, { "PId" : "Carol@gmail.com" }, { "PId" : "Carol@gmail.com" } ] }
- Related Articles
- How to aggregate two collections where a field from one collection is greater than the other in MongoDB?
- How can I aggregate collection and group by field count in MongoDB?
- Project specific array field in a MongoDB collection?
- Matching an array field that contains any combination of the provided array in MongoDB?
- Calculate frequency of duplicate names from NAME field using MongoDB aggregate?
- MongoDB aggregate to get the count of field values of corresponding duplicate names?
- Check duplicates of certain field for inner array in MongoDB
- Reverse array field in MongoDB?
- How to check empty field in a MongoDB collection?
- Export specified field of a collection in mongodb / mongodump to file?
- MongoDB slice array in populated field?
- Get distinct levels of array field in MongoDB?
- Add new field to every document in a MongoDB collection?
- Add field that is unique index to collection in MongoDB?
- MongoDB query for a single field

Advertisements