db.collection.aggregate([{"$group" : {"_id": "$fieldA", "count": {"$sum": 1}}},{"$match":{"_id" :{"$ne":null}, "count" : {"$gt":1}}},{"$project": {"fieldA":"$_id", "_id" : 0}}]);
I am currently using this code to find duplicates however I actually want to print fieldB and not fieldA. By using the above I believe I have created a subset? Is there a simple way to print only fieldB?
ie. I want to print {"_id":0, "fieldB":1}
The only method I can think of is naming this subset and then joining with the original and then printing just fieldB but that doesn't seem efficient.
CodePudding user response:
To see the last fieldB you can do something like this:
$last: playground1 ( But here if there is different fieldB per duplicated fieldA you will not find out )
Or to see the all fieldB for the duplicated filedA you can do something like this:
$push: playground2
db.collection.aggregate([
{
"$group": {
"_id": "$fieldA",
"count": {
"$sum": 1
},
fieldB: {
$push: "$fieldB"
}
}
},
{
"$match": {
"_id": {
"$ne": null
},
"count": {
"$gt": 1
}
}
},
{
"$project": {
"fieldA": "$_id",
"fieldB": 1,
"_id": 0
}
}
])