简体   繁体   中英

Run shell command in mongo aggregation using python file

I need to run a mongo aggregation command in the shell using python script. How can I achieve this? This is my try

import subprocess 
printj = 'aggregation_query'  
cmd ='mongo --quiet mydb --eval',"'",printj,"'",' > output_traffic.json'  
subprocess.call(cmd)

It gives an error

File "shellcmd.py", line 15, in <module>
    subprocess.call(cmd)
  File "/usr/lib64/python2.7/subprocess.py", line 524, in call
    return Popen(*popenargs, **kwargs).wait()
  File "/usr/lib64/python2.7/subprocess.py", line 711, in __init__
    errread, errwrite)
  File "/usr/lib64/python2.7/subprocess.py", line 1308, in _execute_child
    raise child_exception

Don't run the mongo shell using subprocess , just import pymongo and connect to the MongoDB server directly. Examples of running aggregation queries from PyMongo are in the documentation .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM