Mongoexport is a utility to allow exporting of your data to JSON by default or optionally CSV. Any subdocument information will be, as you have noticed output as JSON as it is the only valid representation of any data that is not a top level field. Thus it is designed for the basic use cases.
For anything else you will need to program your own solution, reading the data and transforming to CSV output.
If at all possible, can you rethink the structure of the data in the first place.The structure under data
as keyed sub-documents doesn't make any sense. If it were an array, you could at least get half the job done using the Aggregation Framework.
{
"_id": "99",
"page_id": NumberLong(1122334455),
"data": [
{
"item_id": NumberLong(123456789),
"item_name": "item1"
},
{
"item_id": NumberLong(987654321),
"item_name": "item2"
},
],
"last_updated_utc": ISODate("2013-12-19T13:17:43.994Z")
}
This can be transformed with aggregation as:
db.sample.aggregate([
{$unwind: "$data"},
{$project: {
page_id: 1,
item_name: "$data.item_name",
last_updated_utc: 1
}}
])
which yields
[
{
"_id" : "99",
"page_id" : NumberLong(1122334455),
"last_updated_utc" : ISODate("2013-12-19T13:17:43.994Z"),
"item_name" : "item1"
},
{
"_id" : "99",
"page_id" : NumberLong(1122334455),
"last_updated_utc" : ISODate("2013-12-19T13:17:43.994Z"),
"item_name" : "item2"
}
],
Which is very much the denormalized form and gives us more hope of converting to CSV.
The problem with the structure here is since each sub-document in data
is keyed and data
itself is not an array, you are forced to programatically traverse each element. This also limits the utility of query functions that can be performed as each sub-document needs to be explicitly named.
So there is no tool, and your data is not making things easier. Change it if you can.