MongoDB: Error executing stored JavaScript function

可紊 提交于 2019-12-04 07:32:30

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)} } )         
> db.eval( "foo(3)" )                                                                               
{ "value" : "DBQuery: admin.system.indexes -> undefined" }

just as you describe.
And after:

> db.system.js.save( { _id : "foo", value: function(n){return db.system.indexes.find().limit(n).toArray()} } )
> db.eval( "foo(3)" )                                                                                         
[
        {
                "name" : "_id_",
                "ns" : "admin.system.users",
                "key" : {
                        "_id" : 1
                }
        },
        {
                "name" : "user_1",
                "ns" : "admin.system.users",
                "key" : {
                        "user" : 1
                },
                "unique" : false
        },
        {
                "name" : "_id_",
                "ns" : "admin.whee",
                "key" : {
                        "_id" : 1
                },
                "v" : 0
        }
]
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!