Currently, I am utilizing a MongoDB query within a JavaScript function. The script is structured as follows:
var continous = ['b', 'e', 'LBE'];
continous.forEach(e => izracun(e));
function izracun(atr) {
var query1 = { "$match": { [atr]: {"$ne" : -1} } };
var query2 = { "$group": { "_id": atr, "avg": { "$avg": "$"+[atr] }, "stdev": { "$stdDevPop": "$"+[atr] }, "nonMissing": { "$sum": 1 }}};
db.ctg.aggregate([query1, query2]);
}
Upon executing this script in the mongo shell using load("script.js")
, the shell responds with "true". Even when I attempted to use fixed parameter values within the function instead of passing arguments, I did not achieve the expected results (only "true" is returned) which should resemble the example below:
{ "_id" : "b", "avg" : 878.4397930385701, "stdev" : 893.8744489449962, "nonMissing" : 2126 }
If I log the query and run it directly in the mongo shell, it functions correctly.
What could be the issue here?
EDIT: I attempted to manage the promise with:
db.ctg.aggregate([query1, query2]).then(function (results) { //this block will run synchronsly to the aggregate statement
console.log(results);
});
However, I encountered the following error:
uncaught exception: TypeError: db.ctg.aggregate(...).then is not a function