我无法访问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
有没有办法让 [对象BSON]成为字符串?
Mongo db 3.0.8版robomongo版本Robomongo 0.9.0-RC8
答案 0 :(得分:2)
CSV是平面2d矩阵,无法容纳复杂结构。您需要将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
}}
]);
如果您的数组有多个元素,或者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"),
....
}
答案 1 :(得分: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#");
});
输出不会被格式化为一个。 '打印'命令总是用新行写!因此,在获得输出后,您将不得不使用编辑器(如记事本++)格式化它。
上次输出
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