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

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -