Is it possible to write to mongodb console in javascript execution?

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.


Solution 1:

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
>

Solution 2:

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.

Solution 3:

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

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