MongoDB Stored Javascript from Node.JS

Node.JS from Javascript Stored MongoDB

This is just a simple example of creating and using Stored Javascripts in MongoDB and calling it from Node.js.

1. Already there

You should have installed mongodb, node.js and it also should running.

Add in your MongoDB 2 Datasets with a column ertrag as Int.


2. Create Node.js app

In your node.js app.js create following:

var Db = require('mongodb').Db;
var Server = require('mongodb').Server;

var db = new Db('test', new Server('', 27017, {})); (err, pClient) {
    db.eval("gesamtertrag()", function (error, result) {


3. Create Stored Javascript

in MongoVue create a new Stored Javascript with name "gesamtertrag" with following code:

function () {
	var a = 0;
	 function(data) {
	 	a += data.ertrag;
	  	//print( + "<p>"); 
	 } );
	 return a;

if you now start the app.js you will see the result.

Comments are closed