[Solved] mongodb print json without whitespace i.e. unpretty json

I’m using mongodb 2.2.0 and am trying to print json in a single line as opposed to “pretty” printing using printjson() or find().pretty(). i.e. I need the documents listed in json format as done by just running the command db.collection.find().limit(10), but I need it done using a cursor in a javascript file as follows:

var cursor = db.collection.find().sort({_id:-1}).limit(10000);
while(cursor.hasNext()){
    //printNonPrettyJson(cursor.next()); //How???!
}

print() doesn’t do the job, it just prints some gibberish about the object identifier.

The reason I want this is because I’m calling the javascript file from the console and then passing the output to a file as follows:

mongo mydatabase myjsfile.js >> /tmp/myoutput.txt

EDIT: I want the output as follows:

> db.zips.find().limit(2)
{ "city" : "ACMAR", "loc" : [ -86.51557, 33.584132 ], "pop" : 6055, "state" : "A
L", "_id" : "35004" }
{ "city" : "ADAMSVILLE", "loc" : [ -86.959727, 33.588437 ], "pop" : 10616, "stat
e" : "AL", "_id" : "35005" }
>

and not like:

> db.zips.find().limit(2).pretty()
{
        "city" : "ACMAR",
        "loc" : [
                -86.51557,
                33.584132
        ],
        "pop" : 6055,
        "state" : "AL",
        "_id" : "35004"
}
{
        "city" : "ADAMSVILLE",
        "loc" : [
                -86.959727,
                33.588437
        ],
        "pop" : 10616,
        "state" : "AL",
        "_id" : "35005"
}
>

as is given by all the other methods. Again, I need this using a cursor object.

Solution #1:

var cursor = db.collection.find().sort({_id:-1}).limit(10000);
while(cursor.hasNext()){
    printjsononeline(cursor.next());
}
Respondent: Plasty Grove

Solution #2:

Try print(tojson()) – there’s an example of printing using a cursor in the MongoDB docs.

    var myCursor = db.inventory.find( { type: 'food' } );
    var myDocument = myCursor.hasNext() ? myCursor.next() : null;

    if (myDocument) {
        var myItem = myDocument.item;
        print(tojson(myItem));
    }
Respondent: MaratC

Solution #3:

You can always do a JS hack for this:

> db.tg.find().forEach(function(doc){ print(tojson(doc).replace(/(
|
|
|s)/gm, '')); })
{"_id":ObjectId("511223348a88785127a0d13f"),"a":1,"b":1,"name":"xxxxx0"}

Not pretty but works

Respondent: Michael Marr

Solution #4:

With “sort” and “limit” , results can be customised.
with mongoexport –type=csv , result can be printed into csv file, which can be read in xls or in one line.

Respondent: Sammaye

Solution #5:

if each item has {} brackets and there are no others then split it up on the brackets using a regular expression.

This would split it up into {..} {..} items. But if there are nested {} it would not work.

var res = s.match(/{(.|s)*?}/g);
if(res) for(var x=0;x<res.length;x++){
    // print  res[x].replace(/s+/g," ");// w/o spaces
}
Respondent: Shiva

Solution #6:

Here is what I use from the command line

mongoexport -d $dbname -c $collection -q '{ "id" : -1 }'

Not sure you can /sort /limit it

Respondent: QuentinUK

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Leave a Reply

Your email address will not be published.