Question

I am running the following command and I want to see the output in my out.log file but it is not printing anything.

   /tmp/mongodb/bin/mongo mydatabase_name /tmp/mongodb/scripts/test.js >> out.log

These are the contents in my test.js

db.system.js.save (
{   _id:"run_job",
    value: function() {

    console.log("in function");
    print("in function>>");
    db.myUser.find().forEach(
    function(eachuser){
        console.log("eachuser id" + eachuser._id);
        var lower_id = eachuser._id.toLowerCase();
        console.log("Lowercase id" + lower_id);
    });     
    }
}
 );

It give error : invoke failed: JS error : console is not defined. So it removed the console.log. But it still does not print using the print function. Thanks.

Was it helpful?

Solution

There is no console object defined in the MongoDB shell.

You'll need to use the print and printjson functions that are globally available.

print("Hi! I'm in your log!");

Anything printed will be emitted to the current log file. If there is no log file defined in your mongo configuration, then it will be emitted to the screen/stdout, which would allow you to pipe the results to a file if you wanted (or you might just want to use the log file generated by MongoDB).

If you had a bunch of code you were using that had console.log, you could build a polyfill that redirected the output via a print and printjson.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top