Author Archive: jamesshaji

Adding user defined function to MongoDB

Few ways to reduce repetitive actions by writing Javascript functions in mongodb

For example let me try to write a function to get data in a collection “testcol2”, I can write below line in mongoDB client

<em>   {     _id: "getdatafromdb",     value : function (){  return (db.testcol2.find());}   })</em>

Above function gets saved in the database. Now to make use of the function we have to load it in memory, we can achieve this using “db.loadServerScripts()“.

Now call “getdatafromdb()“, we can call this while performing querying also.

Disclaimer: All published content views of author.