PHP Snippet 1:
db.yourCollection.aggregate([
{ "$group": {
"_id": { "yourDuplicateKey": "$yourDuplicateKey" },
"dups": { "$push": "$_id" },
"count": { "$sum": 1 }
}},
{ "$match": { "count": { "$gt": 1 } }}
]).forEach(function(doc) {
doc.dups.shift();
db.yourCollection.remove({ "_id": {"$in": doc.dups }});
});
PHP Snippet 2:
$DataForDB = array( "AdmissionNo" => $admissionNo,
"StudentName" => $StudentName, "ParentName" => $ParentName);
if(empty($Coll->findOne(array("StudenName" => $StudentName, "ParentName" => $ParentName)))){
$Coll->insertOne($DataForDB);
}
PHP Snippet 3:
db.yourCollection.aggregate([
{ "$group": {
"_id": { "yourDuplicateKey": "$yourDuplicateKey" },
"dups": { "$push": "$_id" },
"count": { "$sum": 1 }
}},
{ "$match": { "count": { "$gt": 1 } }}
], { allowDiskUse: true } )
.forEach(function(doc) {
doc.dups.shift();
db.yourCollection.remove({ "_id": {"$in": doc.dups }});
});