169

Probably a dumb question. Experimenting with Mongo shell.

I want to do something like:

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

In other words, how can I output to the console when evaluating JS functions from within Mongo shell. I intend some admin / populate script that I'd like to output some nicety info.

I've tried console.log, debug.log, log the obvious ones..

2 Answers2

316

I've found the answer, it is simply print

print("sweetnesss");
Max Filippov
  • 2,024
  • 18
  • 37
5

I usually use scripts when interacting with the shell, so I wrote a basic Logging object that I "load(script)" into the shell, and then use the Logging object to call logging levels (debug,info,warn,error). The Logger object does use 'print' and 'printjson' at it's core. Also, it contains a basic sprintf, padding, record formatting, etc. If you are going to be doing any signifigant amount of script work with mongodb, I recommend this route. Debugging mongodb scripts feels like being back at intro to programming and debugging with print statements. Configurable logging makes this a little better, but I really miss being able to step through the code.

gbegley
  • 2,609
  • 5
  • 29
  • 41
  • +1 for the comment about print debugging, couldn't agree more. Are you sharing your logging code on GitHub or somewhere? I was considering doing that myself, but I haven't got much code to share yet. I forked https://github.com/rsdoiel/mongo-modules but haven't added anything yet. – Mark Edington Oct 13 '13 at 14:17
  • I'll try to dig this up. – gbegley Oct 14 '13 at 21:36
  • gbegley: Excellent, looks like something that will be useful for me. Appreciate you taking the time to provide this to the mongodb community. – Mark Edington Oct 19 '13 at 14:02