53

I'm learning about map-reduce functionality of mongodb. My first tests doesn't work as I expected and I want to know how is it working.

Is there any way to write to mongodb console from javascript functions so I can inspect it?

I tried console.log("...") but it doesn't work.

I will ask later about my tests if there isn't any way to do it.

Cœur
  • 37,241
  • 25
  • 195
  • 267
francadaval
  • 2,451
  • 3
  • 26
  • 36

4 Answers4

91

You have to use 'print( "anything .." )' or printjson to display objects.

andrey@andrey:~$ mongo
MongoDB shell version: 2.0.2
connecting to: test
> object = { "name" : "any name .." , "key" : "value" }
{ "name" : "any name ..", "key" : "value" }
> printjson ( object )
{ "name" : "any name ..", "key" : "value" }
> print ( "hello world" )
hello world
>
  • I'm on Mac and this worked. If it's not working, it's because you're doing something else wrong. Double-check your file paths, permissions, etc. – CommaToast Jun 03 '14 at 19:28
7

I guess from map/reduce functions you need to insert your debug messages into some logs collection:

var map = function() {
  //some staff here
};

var reduce = function(key, values) {
  db.mr_logs.insert({message: "Message from reduce function"});
  //some staff here
};


res = db.items.mapReduce(map, reduce,{ query : {}, out : 'example1' })

After this you can find your debug results in mr_logs collection.

db.mr_logs.find();

As for print it seems not printing output to console when you are in map or reduce functions.

Andrew Orsich
  • 52,935
  • 16
  • 139
  • 134
5

there is a super easy workaround in map-reduce environment.

How to get print output for debugging map/reduce in Mongoid

Community
  • 1
  • 1
Jerry Chen
  • 426
  • 5
  • 10
-2

You can just write the name of the function / object like so:

>fn = function (){return12;}
>fn
function (){return12;}
>

Try it here: http://try.mongodb.org/

alessioalex
  • 62,577
  • 16
  • 155
  • 122