【问题标题】:roboMongo export to csv output shows bsonroboMongo 导出到 csv 输出显示 bson
【发布时间】:2017-03-08 13:32:58
【问题描述】:

我无法访问 mongodb 机器。所以我无法运行 mongoexport 命令。因此,我试图将我的查询输出转换为 csv 格式。

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 格式)- 此处仅提供 1 行

{
    "_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)

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

有没有办法让 [Object BSON] 变成字符串?

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

【问题讨论】:

  • mongoexport 通过网络工作。如果你可以用 robomongo 连接到 mongodb,你应该可以用 mongoexport 连接到它。
  • 我无法访问已安装的机器(通过 ssh 进入盒子)!但是 robomongo 可以通过端口访问数据库,因为网络团队启用了 IP:PORT :)
  • 你不需要 ssh。在使用相同 ip:port 选项运行 robomongo 的同一台机器上运行 mongoexport。
  • 哦..我会试试的。

标签: json mongodb csv robo3t


【解决方案1】:

CSV 是平面二维矩阵,不能容纳复杂的结构。你需要project你的文档到顶级原语。

对于您的文档,它必须类似于以下内容(Mongo 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 个元素,或者 mongo 版本

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"),
    ....
}

【讨论】:

  • 谢谢亚历克斯。但我得到异常:断言:命令失败“errmsg”:“异常:无效运算符'$arrayElemAt'”,“代码”:15999,| Mongo 数据库版本 3.0.8 | robomongo 版本 Robomongo 0.9.0-RC8
  • 很公平,我已经为古代版本的 db 添加了一个示例。
  • 我用另一种方式尝试过,效果很好。谢谢您的帮助。我已经添加了答案。
【解决方案2】:

这对我有用。好吧,我不确定这是否是最好的方法。正如@Alex 建议的那样,可能还有其他方法。我在代码中添加了 cmets,以便于阅读和理解。

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

【讨论】:

    猜你喜欢
    • 2017-07-24
    • 2015-01-17
    • 2022-01-21
    • 2021-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多