mongosh执行脚本时print语句输出未显示的问题求助
解决mongosh执行脚本时print输出不显示的问题
针对你遇到的mongosh执行脚本后仅显示连接信息、脚本内print语句无输出的问题,可以尝试以下几种解决方法:
1. 替换输出语句为console.log
mongosh基于Node.js环境,使用console.log比原生print更稳定,能确保输出被正常捕获。修改脚本如下:
use espaidoc; // List current indices of the "Audit" collection console.log("Current indices of the 'Audit' collection:"); db.Audit.getIndexes().forEach(index => console.log(JSON.stringify(index, null, 2))); // Remove all indices for the "Audit" collection db.Audit.dropIndexes(); console.log("All indices dropped for the 'Audit' collection."); // List updated indices of the "Audit" collection console.log("Updated indices of the 'Audit' collection:"); db.Audit.getIndexes().forEach(index => console.log(JSON.stringify(index, null, 2)));
2. 调整mongosh命令参数
在执行命令时添加--verbose参数,强制输出所有脚本运行时的信息;同时建议直接在命令行指定目标数据库espaidoc,避免脚本内use语句可能带来的上下文切换问题:
mongosh localhost:27117/espaidoc -u $MONGODB_USER -p $MONGODB_PASS --authenticationDatabase admin --verbose --file audit-indices.js
3. 验证脚本执行状态
在脚本末尾添加明确的执行完成提示,确认脚本是否被完整加载并执行:
console.log("=== Script execution finished ===");
如果这条提示仍不显示,需要检查脚本文件路径是否正确,或者当前用户是否有读取该文件的权限。
内容的提问来源于stack exchange,提问作者Jordi
相关产品推荐
相关产品推荐

