Skip to content

Commit

Permalink
修复mongodb不显示执行结果的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
czxin788 committed May 31, 2022
1 parent 3d31f13 commit 70dfa35
Showing 1 changed file with 3 additions and 0 deletions.
3 changes: 3 additions & 0 deletions sql/engines/mongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,12 @@ def exec_cmd(self, sql, db_name=None, slave_ok=''):
if self.user and self.password and self.port and self.host:
msg = ""
auth_db = self.instance.db_name or 'admin'
# 因为用mongo load方法执行js脚本,所以需要重新改写一下sql,以便回显js执行结果
sql = 'var result = ' + sql + '\nprintjson(result);'
# 因为要知道具体的临时文件位置,所以用了NamedTemporaryFile模块
with tempfile.NamedTemporaryFile(suffix=".js", prefix="mongo_", dir='/tmp/', delete=True) as fp:
fp.write(sql.encode('utf-8'))
fp.seek(0) # 把文件指针指向第一个,这样内容才能落到磁盘文件上
try:
if not sql.startswith('var host='): #在master节点执行的情况
cmd = "{mongo} --quiet -u {uname} -p '{password}' {host}:{port}/{auth_db} <<\\EOF\ndb=db.getSiblingDB(\"{db_name}\");{slave_ok}load('{tempfile}')\nEOF".format(
Expand Down

0 comments on commit 70dfa35

Please sign in to comment.