0

I'm trying to use MongoDB server-side JavaScript in my nodejs/node-mongodb-native project and just interested how could I save my custom functions in global context of MongoDB and get access to them from db.eval script?

Let's say I have the following unit function:

var myDocumentUtils = {
    doStuff: function (doc) {
       // do stuff with doc ...
       return doc;      
    }
}

And I have the following 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 to global myDocumentUtils object     
    db.myCollection.save(doc);

    return doc;
};

I execute processDocument function from my nodejs application like the following:

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

So my question is how to save myDocumentUtils in global MongoDB V8 context to be accessible in db.eval function?

1 Answer 1

1

Add the second parameter to processDocument as below:

function processDocument (id, myDocumentUtils) {
    var doc = db.myCollection.findOne({ _id : ObjectId(id)});

    doc = myDocumentUtils.doStuff(doc);   // need access to global myDocumentUtils object     
    db.myCollection.save(doc);

    return doc;
};

Then write db.eval() like this way:

db.eval(function() {
    return processDocument.apply(this, arguments);
}, "54382cb3233283cd3331ca1d", myDocumentUtils);

For your environment, you can add the call back just behind the last parameter myDocumentUtils.


APPEND ---------------------

store below tow functions into db.system.js :

function getMyDocumentUtils() {
    return myDocumentUtils = {
            doStuff: function (doc) {
               // do stuff with doc ...
               return doc;      
            }
        };
}

function processDocument (id) {
    var doc = db.myCollection.findOne({ _id : ObjectId(id)});

    var myDocumentUtils = getMyDocumentUtils(); // added line

    doc = myDocumentUtils.doStuff(doc);   // need access to global myDocumentUtils object     
    db.myCollection.save(doc);

    return doc;
};

Then call db.eval() as your original style.

Sign up to request clarification or add additional context in comments.

8 Comments

thanks for the response but I can't pass JavaScript object to eval functions because I use nodejs. Is there any solutions yet?
@Erik, as you are able to store processDocument into database, could you also store another function used to return myDocumentUtils? This function can be called by processDocument.
could you provide some example please because I little don't understand your suggestion :(
@Wizardt Thanks you. I do understand your advice and this very cool for me. As I also understand getMyDocumentUtils returns function that evaluates on every call? It it possible to evaluate this function one for performance reasons?
Can I store two javascript functions in db.system.js?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.