Skip to content

Instantly share code, notes, and snippets.

@amircloner
Created February 8, 2023 06:36
Show Gist options
  • Save amircloner/c7cdeb484956d21096ddbd0b756e8e6e to your computer and use it in GitHub Desktop.
Save amircloner/c7cdeb484956d21096ddbd0b756e8e6e to your computer and use it in GitHub Desktop.
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