Plasty Grove
Plasty Grove

Reputation: 2877

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.

Upvotes: 37

Views: 49582

Answers (6)

Shiva
Shiva

Reputation: 39

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.

Upvotes: 2

MaratC
MaratC

Reputation: 6889

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

Upvotes: 91

Perki
Perki

Reputation: 706

Here is what I use from the command line

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

Not sure you can /sort /limit it

Upvotes: 0

QuentinUK
QuentinUK

Reputation: 3077

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
}

Upvotes: 0

Sammaye
Sammaye

Reputation: 43884

You can always do a JS hack for this:

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

Not pretty but works

Upvotes: 3

Michael Marr
Michael Marr

Reputation: 2099

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));
    }

Upvotes: 5

Related Questions