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

JavascriptMongodb

Javascript Problem Overview


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.

Javascript Solutions


Solution 1 - Javascript

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

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

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

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

Solution 4 - Javascript

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/

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionfrancadavalView Question on Stackoverflow
Solution 1 - Javascriptuser862010View Answer on Stackoverflow
Solution 2 - JavascriptAndrew OrsichView Answer on Stackoverflow
Solution 3 - JavascriptJerry ChenView Answer on Stackoverflow
Solution 4 - JavascriptalessioalexView Answer on Stackoverflow