Created
February 8, 2023 06:36
-
-
Save amircloner/c7cdeb484956d21096ddbd0b756e8e6e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
db.orders.aggregate([ | |
{ | |
$group: { | |
// collect ids of the documents, that have same emrooz_idue | |
// for a given key ('emrooz_id' prop in this case) | |
_id: '$emrooz_id', | |
ids: { | |
$push: '$_id' | |
}, | |
// count N of duplications per key | |
totalIds: { | |
$sum: 1, | |
} | |
} | |
}, | |
{ | |
$match: { | |
// match only documents with duplicated emrooz_idue in a key | |
totalIds: { | |
$gt: 1, | |
}, | |
}, | |
}, | |
{ | |
$project: { | |
_id: false, | |
documentsThatHaveDuplicatedValue: '$ids', | |
} | |
}, | |
{ | |
$lookup: { | |
// note, you need to use same collection name here | |
from: 'orders', | |
localField: 'documentsThatHaveDuplicatedValue', | |
foreignField: '_id', | |
as: 'documentsThatHaveDuplicatedValue' | |
} | |
} | |
]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment