Reputation: 377
how can I detecte duplication even if the fields type are not the same ?
{id : 1 , price : 5}
{id : 2 , price : "6"}
{id : 3 , price : "5"}
so duplicates are
{id : 1 , price : 5}
{id : 3 , price : "5"}
Upvotes: 2
Views: 252
Reputation: 45432
You can use $substr
to do the conversion to String going from index 0 to -1 (rest of the string). :
db.duplicates.aggregate(
[{
"$project": {
id: 1,
price: { $substr: ["$price", 0, -1] }
}
}, {
"$group": {
"_id": "$price",
"count": { "$sum": 1 },
"item": { $push: "$$ROOT" }
}
}, {
"$match": {
"_id": { "$ne": null },
"count": { "$gt": 1 }
}
}]
)
The rest of the aggregation $group
by $price
to count the number of occurences and match items that have a count > 1 (duplicates). In item
, you have the initial items that are duplicates :
{ "_id" : "5", "count" : 2, "item" : [ { "_id" : ObjectId("58616dc177b68a6c54252bc8"), "id" : 1, "price" : "5" }, { "_id" : ObjectId("58616dc177b68a6c54252bca"), "id" : 3, "price" : "5" } ] }
To count the number of distinct fields duplicated, add :
{
"$group": {
"_id": null,
"totalCount": {
"$sum": 1
}
}
}
Upvotes: 1