2017-03-07 75 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