I have my JS file.
I have a variable declared at the top called:
var aCollection = db.myCollection;
I use this variable to refer to the collection. It’s easy, since I can change the name of the collection to process another collection.
Now I want to use this in a function, like this:
fn1 = function(_id) {
// use _id and aCollection
// i get an error if I use "aCollection"
}
And I call the above function like this:
db.eval(fn1, "245");
db.evalexecutesfn1on the server in a separate context so it doesn’t have access to the global context of its containing script. You’d have to changefn1to acceptaCollectionas a parameter and then pass that parameter into yourdb.evalcall.