@bentael I write this run_me.js file and run it with node run_me.js command, that iterate over all nodebb posts and run a JavaScript function on content filed and update it.
how to efficient it using db.collection('objects').updateMany() ?
content of run_me.js file:
var MongoClient = require('mongodb').MongoClient;
var assert = require('assert');
var ObjectId = require('mongodb').ObjectID;
//'mongodb://<dbuser>:<dbpassword>@<mongodb host>:<mongodb port>/<dbName>'
var url = '';
var converter = require("bbcode-to-markdown");
var convert = function(str){ // alter it and put your desire JavaScript cods.
if ( typeof str == 'string' ){
str = str.toString().toLowerCase();
return converter(str);
}
else
return str;
}
var updatePosts = function(db, callback){
var cursor =db.collection('objects').find( { _key:{ $regex: /post:/i } } );
cursor.each(function(err, doc) {
assert.equal(err, null);
if (doc != null ) {
var newcontent = convert( doc.content );
db.collection('objects').updateOne({ _key: doc._key }, { $set: { "content": newcontent } });
console.log("after update --"+doc._key);
} else {
callback();
}
});
}
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
updatePosts(db, function() {
db.close();
});
});