Created
January 25, 2019 22:06
-
-
Save barisusakli/6cce26bbb2b235ea3d083be0755d90a7 to your computer and use it in GitHub Desktop.
fix _key_value unique values
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*globals require, console, process */ | |
'use strict'; | |
var nconf = require('nconf'); | |
var async = require('async'); | |
nconf.file({ | |
file: 'config.json' | |
}); | |
nconf.defaults({ | |
base_dir: __dirname, | |
views_dir: './build/public/templates', | |
}); | |
var db = require('./src/database'); | |
db.init(function(err) { | |
if (err) { | |
console.log("NodeBB could not connect to your database. Error: " + err.message); | |
process.exit(); | |
} | |
fixValues(function (err) { | |
if (err) { | |
console.error(err); | |
process.exit(); | |
} | |
console.log('done'); | |
process.exit(); | |
}); | |
}); | |
function fixValues(callback) { | |
var client = db.client; | |
async.waterfall([ | |
function (next) { | |
client.collection('objects').aggregate([ | |
{ | |
$match: { | |
_key: { $exists: true }, | |
value: { $exists: true }, | |
score: { $exists: true } | |
}, | |
}, | |
{ | |
$group: { | |
_id: { _key: '$_key', value: '$value' }, | |
count: { $sum: 1 } | |
} | |
}, | |
{ | |
$match: { | |
count : {$gt: 1} | |
} | |
} | |
], { allowDiskUse: true }).toArray(next); | |
}, function (data, next) { | |
async.eachSeries(data, function (item, next) { | |
var dummyArray = new Array(item.count - 1); | |
async.eachSeries(dummyArray, function (dummy, next) { | |
console.log('removing', { _key: item._id._key, value: item._id.value }); | |
client.collection('objects').deleteOne({ _key: item._id._key, value: item._id.value }, next); | |
}, next); | |
}, next); | |
}, | |
], callback); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment