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.

4 Answers 4

93

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
>
Sign up to request clarification or add additional context in comments.

1 Comment

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.
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.

Comments

5

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

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

Comments

-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/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.