繁体   English   中英

使用python文件在mongo聚合中运行shell命令

[英]Run shell command in mongo aggregation using python file

我需要使用python脚本在shell中运行mongo聚合命令。 我该如何实现? 这是我的尝试

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

它给出了一个错误

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

不要运行mongo使用shell subprocess ,只需要导入pymongo,直接连接到MongoDB服务器。 文档中提供了从PyMongo运行聚合查询的示例

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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