Skip to content Skip to sidebar Skip to footer

Techniques For Storing Libraries In Mongodb's System.js

Are there any reliable techniques for storing prototype-based libraries/frameworks in mongoDB's system.js? I came across this issue when trying to use dateJS formats within a map-

Solution 1:

Every query using JS may reuse or get a brand new JS context, on which stored JS objects are loaded. In order to do what you want, you need either:

  1. mongod to run the stored code automatically when installing it
  2. mapreduce to have an init method

The first is definitely the more interesting feature. Turns out that mongodb v8 build automatically does it (but not officially supported), but not the official spidermonkey build.

Say you store code like:

db.system.js.save({ _id: "mylib", value: "myprint = function() { print('installed'); return 'installed';" }

Then in v8 you can use myprint() freely in your code, but with SM you would need to call mylib() explicitly.

As a workaround you can create another method:

db.system.js.save({ _id: "installLib", value: "if (!libLoaded) mylib(); libLoaded = true;" }

And call it from your map() function.

Created ticket in order to standardize engines and allow automatic run: https://jira.mongodb.org/browse/SERVER-4450

Post a Comment for "Techniques For Storing Libraries In Mongodb's System.js"