Pla*_*ove 31 javascript mongodb
我正在使用mongodb 2.2.0并试图在一行中打印json而不是使用printjson()
或者"漂亮"打印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???!
}
Run Code Online (Sandbox Code Playgroud)
print()
不做这个工作,只是打印一些关于对象标识符的乱码.
我想要这个的原因是因为我从控制台调用javascript文件,然后将输出传递给文件,如下所示:
mongo mydatabase myjsfile.js >> /tmp/myoutput.txt
Run Code Online (Sandbox Code Playgroud)
编辑:我想要输出如下:
> 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" }
>
Run Code Online (Sandbox Code Playgroud)
而不是像:
> 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"
}
>
Run Code Online (Sandbox Code Playgroud)
正如所有其他方法所给出的那样.同样,我需要使用游标对象.
Mar*_*atC 74
var cursor = db.collection.find().sort({_id:-1}).limit(10000);
while(cursor.hasNext()){
printjsononeline(cursor.next());
}
Run Code Online (Sandbox Code Playgroud)
尝试一下- MongoDB 文档print(tojson())
中有一个使用光标打印的示例。
var myCursor = db.inventory.find( { type: 'food' } );
var myDocument = myCursor.hasNext() ? myCursor.next() : null;
if (myDocument) {
var myItem = myDocument.item;
print(tojson(myItem));
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
32339 次 |
最近记录: |