How can I execute a JS script file from Pymongo?

笑着哭i 提交于 2019-12-07 19:26:22

问题


I have a MapReduce job for my MongoDB database implemented in a Javascript script file. I've tested it from the commandline and Mongo shell (load("MR_stack.js")). Now I'm using Pymongo within a larger application to access the Mongo database. How can I execute my MR_stack.js script from within Pymongo?


回答1:


The database object has an eval method:

http://api.mongodb.org/python/current/api/pymongo/database.html




回答2:


Well, your favorite option is to read existing documentation:

http://api.mongodb.org/python/2.1/examples/map_reduce.html




回答3:


from bson import Binary, Code

You can use this library for running your javascript mapreduce code like this

map = code("""your mapreduce code""")

reducer = code("""your mapreduce code""")

emp = db.orders.map_reduce(mapper, reducer, "moid_details")


来源:https://stackoverflow.com/questions/10230794/how-can-i-execute-a-js-script-file-from-pymongo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!