mongodb打印的json没有空格,也就是不漂亮的json。

时间:2022-07-11 21:45:03

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:

我使用mongodb 2.2.0,并尝试在一行中打印json,而不是使用printjson()或find()的“漂亮”打印。例如,我需要通过运行命令db.collection.find().limit(10)来列出json格式所列出的文档,但是我需要在javascript文件中使用游标来完成:

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.

print()不做这个工作,它只是打印一些关于对象标识符的胡言乱语。

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:

我之所以想要这样做是因为我从控制台调用javascript文件,然后将输出传递给一个文件,如下所示:

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.

和其他方法一样。同样,我需要使用光标对象。

6 个解决方案

#1


57  

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

#2


3  

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

尝试打印(tojson())——在MongoDB文档中有一个使用游标的打印示例。

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

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

#3


2  

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.

通过“排序”和“限制”,结果可以定制。使用mongoexport—type=csv,结果可以打印到csv文件中,可以在xls或一行中读取。

#4


1  

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

不漂亮但作品

#5


0  

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
}

#6


0  

Here is what I use from the command line

下面是我在命令行中使用的内容。

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

Not sure you can /sort /limit it

不确定你是否能/分类/限制它。

#1


57  

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

#2


3  

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

尝试打印(tojson())——在MongoDB文档中有一个使用游标的打印示例。

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

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

#3


2  

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.

通过“排序”和“限制”,结果可以定制。使用mongoexport—type=csv,结果可以打印到csv文件中,可以在xls或一行中读取。

#4


1  

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

不漂亮但作品

#5


0  

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
}

#6


0  

Here is what I use from the command line

下面是我在命令行中使用的内容。

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

Not sure you can /sort /limit it

不确定你是否能/分类/限制它。