MongoDB distinct with array of objects | Getting distinct values from object array MongoDB

Let say we have a collection transactions which contains some document and documents contain payments which is array of objects.
Now my task is to get the unique types in payments array of every document.

Expected Result: Debit Card, Internet Banking, Credit Card, UPI



which is unique values of payment types in transctions collection and the MongoDB query for that is given below:

Here we first unwind the data so that every payments array can be converted to objects and then we are grouping the type of payments document in transctions collection.

Share This:

Leave a Reply

Your email address will not be published. Required fields are marked *