[英]Run shell command in mongo aggregation using python file
I need to run a mongo aggregation command in the shell using python script. 我需要使用python脚本在shell中运行mongo聚合命令。 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. 不要运行mongo
使用shell subprocess
,只需要导入pymongo,直接连接到MongoDB服务器。 Examples of running aggregation queries from PyMongo are in the documentation . 文档中提供了从PyMongo运行聚合查询的示例 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.