MongoDB: Error executing stored JavaScript function
When I run below stored JavaScript function I get errors: > db.system.js.save({_id:"last_n_users", value: function(n){return db.users.find().sort({created_at:-1}).limit(n)}}) > db.eval("last_n_users(10)") Here is the errors: { "value" : "DBQuery: store.users -> undefined" } Why? Please help me? The find() function returns a cursor, which can't be returned from JavaScript. The suggested workaround is to use toArray() to get an array return value. Example ... before : > use admin switched to db admin > db.system.js.save( { _id : "foo", value: function(n){return db.system.indexes.find().limit(n)}