Believe it or not, it's possible to store a "live" Javascript function in a MongoDB collection:
> db.collection.insert({ name: "add1", f: (function(x) { return x + 1 }) })
WriteResult({ "nInserted" : 1 })
> db.collection.findOne({ name: "add1" }).f(123)
124
A "function with closure" (or, more simply, a "closure") is a function which refers to variables which exist outside the function, like incrementX
in the following snippet:
var x = 1;
function incrementX() { x++; }
These functions can be stored in a MongoDB collection as well; they will bind to the scope of the mongo session when they're executed:
> db.collection.insert({
name: "incrementX",
f: (function() { x++; })
})
WriteResult({ "nInserted" : 1 })
> var x = 123;
> db.collection.findOne({ name: "incrementX" }).f()
> x
124
For some unknowable reason, the BSON designers decided to use a different data type for Javascript functions depending on whether they were closed over any variables or not. The plain "Javascript" type is used for functions which don't close over any variables, and "Javascript (with scope)" is used for closures.
Why one would store a Javascript function in a MongoDB collection is… a good question. I'm not sure what the purpose of this feature is; it honestly seems rather dangerous and ill-advised to me. In particular, it'll be difficult to do anything useful with them if you're using a Mongo driver in a non-Javascript language, and using functions in the database exposes you to potential exploits if a malicious user is able to inject a function into your database. If I were you, I'd pretend this feature didn't exist and move on.