
- 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
How to aggregate two lists if at least one element matches in MongoDB?
For this, use $group in MongoDB. Within that, use $unwind, $group, $addToSet, etc. Let us create a collection with documents −
> db.demo456.insertOne( ... { _id: 101, StudentName: ["Chris", "David"] } ... ); { "acknowledged" : true, "insertedId" : 101 } > > db.demo456.insertOne( ... { _id: 102, StudentName: ["Mike", "Sam"] } ... ); { "acknowledged" : true, "insertedId" : 102 } > db.demo456.insertOne( ... { _id: 103, StudentName: ["John", "Jace"] } ... ); { "acknowledged" : true, "insertedId" : 103 } > db.demo456.insertOne( ... { _id: 104, StudentName: ["Robert", "John"] } ... ); { "acknowledged" : true, "insertedId" : 104 }
Display all documents from a collection with the help of find() method −
> db.demo456.find();
This will produce the following output −
{ "_id" : 101, "StudentName" : [ "Chris", "David" ] } { "_id" : 102, "StudentName" : [ "Mike", "Sam" ] } { "_id" : 103, "StudentName" : [ "John", "Jace" ] } { "_id" : 104, "StudentName" : [ "Robert", "John" ] }
Following is the query to aggregate two lists if at least one element matches −
> db.demo456.aggregate([ ... {$unwind:"$StudentName"}, ... {$group:{_id:"$StudentName", combine:{$addToSet:"$_id"}, size:{$sum:1}}}, ... {$match:{size: {$gt: 1}}}, ... {$project:{_id: 1, combine:1, size: 1, combine1: "$combine"}}, ... {$unwind:"$combine"}, ... {$unwind:"$combine1"}, ... {$group:{_id:"$combine", l:{$first:"$_id"}, size:{$sum: 1}, set: {$addToSet:"$combine1"}}}, ... {$sort:{size:1}}, ... {$group:{_id: "$l", combineIds:{$last:"$set"}, size:{$sum:1}}}, ... {$match: {size:{$gt:1}}} ... ])
This will produce the following output −
{ "_id" : "John", "combineIds" : [ 103, 104 ], "size" : 2 }
- Related Articles
- Python program to check if two lists have at least one common element
- C# program to check if two lists have at-least one element common
- How to subset R data frame rows if at least one or more values matches?
- MongoDB Aggregate Second Element from Input Element?
- Get at least one match in list querying with MongoDB?
- To Aggregate Totals in One Group with MongoDB
- How to check if a string has at least one letter and one number in Python?
- Python - Check if two lists have any element in common
- How to aggregate two collections where a field from one collection is greater than the other in MongoDB?
- Find Array Elements Which has at Least One Smaller Element in Java
- How to update after aggregate in MongoDB?
- How to aggregate array documents in MongoDB?
- Make the sample space of tossing two coins simultaneously and find the probability of:1. Exactly two heads2. At least one head3. At least one tail4. At most two tails
- How to use MongoDB Aggregate to sort?
- Program to check every sublist in a list containing at least one unique element in Python

Advertisements