roboMongo 导出到 csv 输出显示 bson
roboMongo export to csv output shows bson
我无法访问 mongodb 机器。所以我不能 运行 mongoexport 命令。因此,我试图将我的查询输出转换为 csv 格式。
在 Robo 中查询Mongo
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 数据库版本 3.0.8 | robomongo 版本 Robomongo 0.9.0-RC8
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 版本 < 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"),
....
}
这对我有用。好吧,我不确定这是否是最好的方法。正如@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
我无法访问 mongodb 机器。所以我不能 运行 mongoexport 命令。因此,我试图将我的查询输出转换为 csv 格式。
在 Robo 中查询Mongo
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 数据库版本 3.0.8 | robomongo 版本 Robomongo 0.9.0-RC8
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 版本 < 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"),
....
}
这对我有用。好吧,我不确定这是否是最好的方法。正如@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