I have created a stored javascript function in my MongoDB instance that counts the number of records in each collection. If I go to my mongo shell and type:
> db.eval("getTotals()");
it works as expected. if I try to call it through mongo like so:
totals = mongoose.connection.db.eval("getTotals()");
console.log(totals);
undefined gets logged. Does anyone see what I am doing wrong here?
eval()and$wherein the MongoDB manual. If you can express your query using the standard MongoDB query operators, they will take advantage of indexes and enable much better concurrency. For your specific case of counts, there are further server-side optimisations in MongoDB 2.4+. JavaScript queries have greater flexibility at the expense of a performance/concurrency penalty.