MongoDB Delete records where group by count -
i have lot of records contains duplicate addresses. basically, want delete records has number of duplicate addresses more 50 less 300.
this how records want delete:
db.directories.aggregate( [ { $group: { _id: { address: "$address" }, total: { $sum: 1 } } }, { $match: { total: { $gt: 50, $lt: 300 } } }, { $sort: { total: -1 } } ], { allowdiskuse: true });
you can use cursor method foreach()
iterate cursor returned aggregate()
method , delete documents using remove()
method, in following example:
var pipeline = [ { $group: { _id: { address: "$address" }, total: { $sum: 1 } } }, { $match: { total: { $gt: 50, $lt: 300 } } }, { $sort: { total: -1 } } ]; var options = { allowdiskuse: true }; db.directories.aggregate(pipeline, options).foreach(function (doc){ var query = {"address": doc._id.address}; db.directories.remove(query); });
Comments
Post a Comment