Javascript mongodb 打印没有空格的 json,即不漂亮的 json

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14752450/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 17:47:34  来源:igfitidea点击:

mongodb print json without whitespace i.e. unpretty json

javascriptmongodb

提问by Plasty Grove

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()or进行“漂亮”打印find().pretty()。即我需要通过运行命令来完成以 json 格式列出的文档db.collection.find().limit(10),但我需要使用 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.

正如所有其他方法所给出的那样。同样,我需要使用游标对象。

回答by MaratC

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

回答by Michael Marr

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

尝试print(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));
    }

回答by Sammaye

You can always do a JS hack for this:

你总是可以为此做一个 JS hack:

> 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

不漂亮但有效

回答by Shiva

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.

使用 "sort" 和 "limit" ,可以自定义结果。使用 mongoexport --type=csv ,结果可以打印到 csv 文件中,可以在 xls 或一行中读取。

回答by QuentinUK

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
}

回答by Perki

Here is what I use from the command line

这是我从命令行使用的

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

Not sure you can /sort /limit it

不确定你可以/排序/限制它