Eerst moet u uw documenten bijwerken en difficultyrating wijzigen en beatmapset_id naar zwevend puntnummer. Om dat te doen, moet je elk document doorlopen met behulp van de .forEach
methode en werk elk document bij met "Bulk"
operaties voor maximale efficiëntie..
var bulk = db.collection.initializeOrderedBulkOp();
var count = 0;
db.collection.find().forEach(function(doc) {
bulk.find({ '_id': doc._id }).update({
'$set': {
'beatmapset_id': parseFloat(doc.beatmapset_id),
'difficultyrating': parseFloat(doc.difficultyrating)
}
});
count++;
if(count % 100 == 0) {
bulk.execute();
bulk = db.collection.initializeOrderedBulkOp();
}
})
if(count > 0) {
bulk.execute();
}
Nu en sinds De "dropDups"-syntaxis voor het maken van indexen is "verouderd" vanaf MongoDB 2.6 en verwijderd in MongoDB 3.0. Dit is hoe je de dups kunt verwijderen.
Het belangrijkste idee hier is om uw document eerst te sorteren op difficultyrating in aflopende volgorde.
bulk = db.collection.initializeUnorderedBulkOp();
count = 0;
db.collection.aggregate([
{ '$sort': { 'difficultyrating': -1 }},
{ '$group': { '_id': '$beatmapset_id', 'ids': { '$push': '$_id' }, 'count': { '$sum': 1 }}},
{ '$match': { 'count': { '$gt': 1 }}}
]).forEach(function(doc) {
doc.ids.shift();
bulk.find({'_id': { '$in': doc.ids }}).remove();
count++;
if(count === 100) {
bulk.execute();
bulk = db.collection.initializeUnorderedBulkOp();
}
})
if(count !== 0) {
bulk.execute();
}
Dit antwoord bedek het onderwerp voor meer details.