How can I execute a JS script file from Pymongo?

2019-02-19 14:58发布

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?

3条回答
可以哭但决不认输i
2楼-- · 2019-02-19 15:34

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")
查看更多
Root(大扎)
3楼-- · 2019-02-19 15:51

Well, your favorite option is to read existing documentation:

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

查看更多
Root(大扎)
4楼-- · 2019-02-19 15:52
登录 后发表回答