- 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
Unwind two arrays from MongoDB
To unwind, use $unwind. The $unwind deconstructs an array field from the input documents to output a document for each element.
Let us create a collection with documents −
> db.demo515.insertOne( ... { ... "details1": [ ... "4700100004" ... ], ... "details2": [ ... "Not Given" ... ], ... "Value1": [ ... "56", ... "45", ... "35", ... ], ... "Value2": [ ... "35", ... "45", ... "56", ... ]} ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e889e51987b6e0e9d18f590") }
Display all documents from a collection with the help of find() method −
> db.demo515.find();
This will produce the following output −
{ "_id" : ObjectId("5e889e51987b6e0e9d18f590"), "details1" : [ "4700100004" ], "details2" : [ "Not Given" ], "Value1" : [ "56", "45", "35" ], "Value2" : [ "35", "45", "56" ] }
Following is the query to unwind two arrays −
> db.demo515.aggregate([ ... { $unwind: { path: "$Value1", includeArrayIndex : "index1" } }, ... { $unwind: { path: "$Value2", includeArrayIndex : "index2" } }, ... { $project: { ... _id : 1, ... details1: 1, ... details2: 1, ... Value1: 1, ... Value2: 1, ... valid: { $eq: ["$index1", "$index2"] } } ... }, ... { $match: { valid: true } } ... ]);
This will produce the following output −
{ "_id" : ObjectId("5e889e51987b6e0e9d18f590"), "details1" : [ "4700100004" ], "details2" : [ "Not Given" ], "Value1" : "56", "Value2" : "35", "valid" : true } { "_id" : ObjectId("5e889e51987b6e0e9d18f590"), "details1" : [ "4700100004" ], "details2" : [ "Not Given" ], "Value1" : "45", "Value2" : "45", "valid" : true } { "_id" : ObjectId("5e889e51987b6e0e9d18f590"), "details1" : [ "4700100004" ], "details2" : [ "Not Given" ], "Value1" : "35", "Value2" : "56", "valid" : true }
- Related Articles
- MongoDB query to unwind two arrays
- MongoDB $unwind to get the count
- What is the $unwind operator in MongoDB?
- Implement MongoDB Aggregate - unwind, group and project?
- Combining unique items from arrays in MongoDB?
- How to get the intersection of two arrays in MongoDB?
- Getting unique values within two arrays in one MongoDB document
- Finding highest value from sub-arrays in MongoDB documents?
- Print uncommon elements from two sorted arrays
- Maximizing Unique Pairs from two arrays in C++
- Finding maximum number from two arrays in JavaScript
- C++ program to find two numbers from two arrays whose sum is not present in both arrays
- Update two separate arrays in a document with one update call in MongoDB?
- How does MongoDB index arrays?
- Projection of arrays to get the first array element from MongoDB documents

Advertisements