2017-03-07 98 views
1

我无法访问mongodb机器。所以我不能运行mongoexport命令。因此,我试图让我的查询输出为CSV格式。roboMongo导出为csv输出显示bson

查询在RoboMongo

var cursor = db.getCollection('fineProduct').find 
(
     {"inbuilt.bookingReference" : { $exists : true }} , 

     {"_id":1, 
     "Reference":1, 
     "inbuilt.bookingReference":1, 
     "inbuilt.status":1, 
     "purchase.fineSegments.departureDatetime":1, 
     "purchase.fineSegments.arrivalDatetime":1, 
     "purchase.fineSegments.product.carriage.type":1, 
     "purchase.fineSegments.pricing.amount":1, 
     "purchase.fineSegments.pricing.currency":1 
     }  
) 
while (cursor.hasNext()) { 
    var record = cursor.next(); 
    var output = ""; 
    for (var i in record) { 
     output += record[i] + ","; 
    }; 
    output = output.substring(0, output.length - 1); 
    print(output); 
} 

查找查询输出(以JSON) - 这里只提供

{ 
    "_id" : 10, 
    "inbuilt" : { 
     "status" : "VALIDATED", 
     "bookingReference" : "2015900051789" 
    }, 
    "purchase" : [ 
     { 
      "fineSegments" : [ 
       { 
        "departureDatetime" : ISODate("2015-09-30T18:35:00.000Z"), 
        "arrivalDatetime" : ISODate("2015-09-30T19:17:00.000Z"), 
        "product" : { 
         "carriage" : { 
          "type" : "House" 
         } 
        }, 
        "pricing" : { 
         "amount" : "339.00", 
         "currency" : "INR" 
        } 
       } 
      ] 
     } 
    ], 
    "vendorReference" : "FIRE" 
} 

输出(在CSV)1列

10,[object BSON],[object BSON],FIRE 
12,[object BSON],[object BSON],FIRE 
13,[object BSON],[object BSON],FIRE 
14,[object BSON],[object BSON],FIRE 
15,[object BSON],[object BSON],FIRE 
17,[object BSON],[object BSON],FIRE 
18,[object BSON],[object BSON],FIRE 
19,[object BSON],[object BSON],FIRE 
20,[object BSON],[object BSON],FIRE 

有没有什么办法让[对象BSON]字符串?

Mongo db version 3.0.8 | robomongo版本Robomongo 0.9.0-RC8

+0

mongoexport作品通过网络。如果你可以用robomongo连接到mongodb,你应该可以通过mongoexport连接到它。 –

+0

我无法访问安装的机器(ssh进入盒子)!但是,robomongo可以通过端口访问数据库,因为它是由网络团队启用IP:PORT的。) – smilyface

+0

您不需要ssh。在运行robomongo的同一台机器上运行mongoexport,并使用相同的ip:port选项。 –

回答

0

这为我工作。那么,我不确定这是否是最好的方式。正如@Alex建议的那样,可能还有其他方法。我在代码中添加了注释,以便轻松阅读和理解。

db.getCollection('fineProduct').find 
(
     {"inbuilt.bookingReference" : { $exists : true }} , 

     {"_id":0, //NOT to print ID 
     "vendorReference":1, //col1 
     "inbuilt.bookingReference":1, //col2 
     "inbuilt.status":1, //col3 
     "purchase.fineSegments.departureDatetime":1, //col4 
     "purchase.fineSegments.arrivalDatetime":1, //col5 
     "purchase.fineSegments.product.carriage.type":1, //col6 
     "purchase.fineSegments.pricing.amount":1, //col7 
     "purchase.fineSegments.pricing.currency":1 //col8 
     }  
) 
.limit(3) //limit to 3 rows (remove this once done) 
.forEach(function (x) { 

    //col1 : "vendorReference" 
    print(x.vendorReference + ","); 

    //col2 : "inbuilt.bookingReference" 
    print(x.inbuilt.bookingReference + ","); 

    //col3 : "inbuilt.status" 
    print(x.inbuilt.status + ","); 

    //col4 : "purchase.fineSegments.departureDatetime" 
    x.purchase.forEach(function (y) { 
     if (y.fineSegments instanceof Array) { 
      y.fineSegments.forEach(function (z) { 
       print(z.departureDatetime + ","); 
      }); 
     } 
    }); 

    //col5 : "purchase.fineSegments.arrivalDatetime" 
    x.purchase.forEach(function (y) { 
     if (y.fineSegments instanceof Array) { 
      y.fineSegments.forEach(function (z) { 
       print(z.arrivalDatetime + ","); 
      }); 
     } 
    }); 

    //col6 : "purchase.fineSegments.product.carriage.type" 
    x.purchase.forEach(function (y) { 
     if (y.fineSegments instanceof Array) { 
      y.fineSegments.forEach(function (z) { 
       print(z.product.carriage.type + ","); // used dot as it is not in array with closed bracket 
      }); 
     } 
    }); 

    //col7 : "purchase.fineSegments.pricing.amount" 
    x.purchase.forEach(function (y) { 
     if (y.fineSegments instanceof Array) { 
      y.fineSegments.forEach(function (z) { 
       print(z.pricing.amount + ","); 
      }); 
     } 
    }); 

    //col8 "purchase.fineSegments.pricing.currency" 
    x.purchase.forEach(function (y) { 
     if (y.fineSegments instanceof Array) { 
      y.fineSegments.forEach(function (z) { 
       print(z.pricing.currency); 
      }); 
     } 
    }); 

    print("#line_end#"); 
}); 

输出不会格式化一个。 'print'命令总是用一个新行写入!因此,获得的输出后,你将不得不将其与编辑器(如记事本++)格式..

最后输出

x1,y1,C,Thu Oct 01 2015,Thu Oct 01 2015,FIRE,233,INR 
x2,y3,A,Thu Oct 01 2015,Thu Oct 01 2015,FIRE,433,US 
x5,y4,B,Thu Oct 01 2015,Thu Oct 01 2015,FIRE,890,INR 
2

CSV是平坦的2d矩阵,不能容纳复杂的结构。您需要将文档project转换为顶级基元。

对于您的文档一定是像以下(蒙戈3.2+):

db.getCollection('fineProduct').aggregate([ 
    {$project: { 
     _id: 1, 
     status: "$inbuilt.status", 
     bookingReference: "$inbuilt.bookingReference", 
     departureDatetime: { "$arrayElemAt": [ 
      { "$map": { 
       "input": { "$slice": [ 
        { "$map": { 
         "input": { "$slice": [ "$purchase", 0, 1 ] }, 
         "as": "el", 
         "in": "$$el.fineSegments" 
        }}, 
        0, 1 
       ]}, 
       "as": "el", 
       "in": { "$arrayElemAt": [ "$$el.departureDatetime", 0 ] } 
      }}, 
      0 
     ]}, 
     arrivalDatetime: { "$arrayElemAt": [ 
      { "$map": { 
       "input": { "$slice": [ 
        { "$map": { 
         "input": { "$slice": [ "$purchase", 0, 1 ] }, 
         "as": "el", 
         "in": "$$el.fineSegments" 
        }}, 
        0, 1 
       ]}, 
       "as": "el", 
       "in": { "$arrayElemAt": [ "$$el.arrivalDatetime", 0 ] } 
      }}, 
      0 
     ]}, 
     ..... etc 
    }} 
]); 

,如果你的阵列有超过1元,或蒙戈版本< 3.2,你首先需要放松他们:

db.getCollection('c').aggregate([ 
    {$unwind: "$purchase"}, 
    {$unwind: "$purchase.fineSegments"}, 
    {$project: { 
     _id: 1, 
     status: "$inbuilt.status", 
     bookingReference: "$inbuilt.bookingReference", 
     departureDatetime: "$purchase.fineSegments.departureDatetime", 
     arrivalDatetime: "$purchase.fineSegments.arrivalDatetime", 
     ..... etc 
    }} 

]); 

这将导致与CSV友好的输出:

{ 
    "_id" : 10.0, 
    "status" : "VALIDATED", 
    "bookingReference" : "2015900051789", 
    "departureDatetime" : ISODate("2015-09-30T18:35:00.000Z"), 
    "arrivalDatetime" : ISODate("2015-09-30T19:17:00.000Z"), 
    .... 
} 
+0

谢谢Alex。但我收到异常:断言:命令失败\t“errmsg”:“异常:无效的操作符'$ arrayElemAt'”,“code”:15999,| Mongo db版本3.0.8 | robomongo版本Robomongo 0.9.0-RC8 – smilyface

+0

不够公平,我已经为古代版本的db添加了一个示例。 –

+0

我以另一种方式尝试过,它运行良好。感谢您的帮助。我已经添加了答案。 – smilyface