mongodb - Mongoexport get property of nested objects in CSV output -


i'm trying csv mongo-db using mongoexport.

my data in 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 i'm using following command:

mongoexport -f _id,page_id,last_updated_utc --query {page_id:1122334455} -d mydatabase -c mycollection --csv 

this gives output:

"99",1122334455,2013-12-19t13:17:43.994z exported 1 record 

the problem need item_name data elements in output. these dynamic array contain no items or many items.

if add data fields (-f) parameter, output json string csv, each object, doesn't using data in future.

what i'm aiming like:

"99",1122334455,2013-12-19t13:17:43.994z,item1 "99",1122334455,2013-12-19t13:17:43.994z,item2 

almost denormalised, or outer-join in sql. data item ids.

is possible? how can item_id csv output?

mongoexport utility allow exporting of data json default or optionally csv. subdocument information be, have noticed output json valid representation of data not top level field. designed basic use cases.

for else need program own solution, reading data , transforming csv output.

if @ possible, can rethink structure of data in first place.the structure under data keyed sub-documents doesn't make sense. if array, @ least half job done using 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 transformed 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 denormalized form , gives more hope of converting csv.

the problem structure here since each sub-document in data keyed , data not array, forced programatically traverse each element. limits utility of query functions can performed each sub-document needs explicitly named.

so there no tool, , data not making things easier. change if can.


Comments

Popular posts from this blog

php - regexp cyrillic filename not matches -

c# - OpenXML hanging while writing elements -

sql - Select Query has unexpected multiple records (MS Access) -