I'm trying to get a CSV from mongo-db by using mongoexport.
My data is in this format:
{
"_id": "99",
"page_id": NumberLong(1122334455),
"data": {
"0": {
"item_id": NumberLong(123456789),
"item_name": "item1"
},
"1": {
"item_id": NumberLong(987654321),
"item_name": "item2"
},
},
"last_updated_utc": ISODate("2013-12-19T13:17:43.994Z")
}
To do this I'm using the following command:
mongoexport -f _id,page_id,last_updated_utc --query {page_id:1122334455} -d mydatabase -c mycollection --csv
This gives the output:
"99",1122334455,2013-12-19T13:17:43.994Z
exported 1 record
The problem is that I need the item_name
from the data
elements in the output. These are a dynamic array which could contain no items or many items.
If I add data
to the fields (-f) parameter, then it will just output this as a JSON string into the CSV, for each object, which doesn't help with using the data in future.
What I'm aiming to get is something like:
"99",1122334455,2013-12-19T13:17:43.994Z,item1
"99",1122334455,2013-12-19T13:17:43.994Z,item2
Almost denormalised, or like an outer-join in SQL. So that its just the data
item IDs.
Is this possible? How can I get the item_id
into the CSV output?