没有空格的mongodb print json,即不开心的json [英] mongodb print json without whitespace i.e. unpretty json
问题描述
我正在使用mongodb 2.2.0并尝试在一行中打印json而不是使用 printjson()
或<$ c $打印漂亮 C>找到()。漂亮()。即我需要以json格式列出的文件,只需运行命令 db.collection.find()。limit(10)
,但我需要使用游标完成在javascript文件中,如下所示:
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()
不做这个工作,只是打印一些关于对象标识符的乱码。
print()
doesn't do the job, it just prints some gibberish about the object identifier.
我想要这个的原因是因为我从控制台调用javascript文件,然后将输出传递给文件,如下所示:
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
编辑:我想要输出如下:
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" }
>
而不是:
> 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.
推荐答案
var cursor = db.collection.find().sort({_id:-1}).limit(10000);
while(cursor.hasNext()){
printjsononeline(cursor.next());
}
这篇关于没有空格的mongodb print json,即不开心的json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!