Monday 15 July 2013

javascript - How to create global objects in MongoBD's V8 engine to be accessible via db.eval? -



javascript - How to create global objects in MongoBD's V8 engine to be accessible via db.eval? -

i'm trying utilize mongodb server-side javascript in nodejs/node-mongodb-native project , interested how save custom functions in global context of mongodb , access them db.eval script?

let's have next unit function:

var mydocumentutils = { dostuff: function (doc) { // stuff doc ... homecoming doc; } }

and have next javascript function stored in db.system.js collection:

function processdocument (id) { var doc = db.mycollection.findone({ _id : objectid(id)}); doc = mydocumentutils.dostuff(doc); // need access global mydocumentutils object db.mycollection.save(doc); homecoming doc; };

i execute processdocument function nodejs application following:

db.eval('processdocument(54382cb3233283cd3331ca1d)', function (err, doc) { if (err) throw err; });

so question how save mydocumentutils in global mongodb v8 context accessible in db.eval function?

add sec parameter processdocument below:

function processdocument (id, mydocumentutils) { var doc = db.mycollection.findone({ _id : objectid(id)}); doc = mydocumentutils.dostuff(doc); // need access global mydocumentutils object db.mycollection.save(doc); homecoming doc; };

then write db.eval() way:

db.eval(function() { homecoming processdocument.apply(this, arguments); }, "54382cb3233283cd3331ca1d", mydocumentutils);

for environment, can add together phone call behind lastly parameter mydocumentutils.

append ---------------------

store below tow functions db.system.js :

function getmydocumentutils() { homecoming mydocumentutils = { dostuff: function (doc) { // stuff doc ... homecoming doc; } }; } function processdocument (id) { var doc = db.mycollection.findone({ _id : objectid(id)}); var mydocumentutils = getmydocumentutils(); // added line doc = mydocumentutils.dostuff(doc); // need access global mydocumentutils object db.mycollection.save(doc); homecoming doc; };

then phone call db.eval() original style.

javascript node.js mongodb node-mongodb-native

No comments:

Post a Comment