Mongo Shell - 控制台/调试日志

132 mongodb

可能是一个愚蠢的问题.尝试使用Mongo shell.

我想做的事情如下:

matt@linuxvm:~/mongodb-linux-i686-1.2.3/bin$ ./mongo
MongoDB shell version: 1.2.3
url: test
connecting to: test
Thu Feb 25 20:57:47 connection accepted from 127.0.0.1:37987 #3
type "help" for help
> function test() { debug.log("hello") }
> test()
Thu Feb 25 20:58:06 JS Error: ReferenceError: debug is not defined (shell):0
Run Code Online (Sandbox Code Playgroud)

换句话说,在从Mongo shell中评估JS函数时,如何输出到控制台.我想要一些管理/填充脚本,我想输出一些精确的信息.

我已经尝试过console.log,debug.log,登录明显的..

小智 259

我找到了答案,很简单 print

print("sweetnesss");
Run Code Online (Sandbox Code Playgroud)

  • printjson(thing)在处理对象时更有用 (82认同)

gbe*_*ley 5

我通常在与 shell 交互时使用脚本,因此我编写了一个基本的 Logging 对象,将其“加载(脚本)”到 shell 中,然后使用 Logging 对象调用日志记录级别(调试、信息、警告、错误)。Logger 对象的核心确实使用了“print”和“printjson”。此外,它还包含基本的 sprintf、填充、记录格式等。如果您打算使用 mongodb 进行大量脚本工作,我推荐此路线。调试 mongodb 脚本感觉就像回到了使用 print 语句进行编程和调试的介绍。可配置的日志记录使这变得更好一些,但我真的很怀念能够单步执行代码的感觉。