To define a function:
Db.system.js.insert ({
_id: "Testconcat",
value:function testconcat (S1, S2) {
return s1 + s2
}
});
To run the function:
Db.eval (' Testconcat ("abc", "123");
Results:
abc123
The prerequisite is that you must have permission to execute the eval function, as detailed in this article:
http://blog.csdn.net/yabingshi_tech/article/details/47660493
--Note that if you define a function directly using the following similar form, after exiting the session, log in again, call the function, and discover that the function disappears.
Example:
> Function Testconcat (S1, s2) {... return s1 + s2
... }
> Testconcat (2,3)
5
Exit
> Testconcat (2,3);
2014-02-19t14:11:10.499+0800 Referenceerror:testconcat is not defined
--This article is mainly referred to from: http://blog.csdn.net/miyatang/article/details/19486797,http://www.2cto.com/database/201503/382391.html.