Search code examples
javascriptmongodbmongodb-shell

mongodb debugging - ReferenceError: console is not defined


I got js file to run within mongodb where I got console.log expression for debugging:

use test;
db.city.find().snapshot().forEach(function(city){
    var Pos = city.Pos;

    if (Pos) 
    {
        longLat = Pos.split(" ");
        console.log("longLat");  // for debugging ----------------------------  
        console.log(longLat);

        lon = longLat[0];
        lat = longLat[1];

        if (lon && lat)
        {
            lon = parseFloat(lon);
            lat = parseFloat(lat);
            longLat = [lon, lat];
            city.longLat = longLat;

        }

    }
    db.orgs.save(city);

})

When I run it...

mongo < /path/to/this/js/file.js

... I got error in the output:

ReferenceError: console is not defined

Is there any way to log intermediate results for debugging purposes?


Solution

  • Use the print or printjson methods instead. These are used to emit just like console.log() within the shell:

        if (Pos) 
        {
            longLat = Pos.split(" ");
            print("longLat");  // for debugging ----------------------------  
            printjson(longLat);