mongodb - mongoexport csv output last array values -
inspired question in server fault https://serverfault.com/questions/459042/mongoexport-csv-output-array-values
i'm using mongoexport export collections csv files, when try target fields last members of array cannot export correctly.
command i'm using
mongoexport -d db -c collection -fieldfile fields.txt --csv > out.csv
one item of collection:
{ "id": 1, "name": "example", "date": [ {"date": ""}, {"date": ""}, ], "status": [ "true", "false", ], }
i can access first member of array writing fields following
name id date.0.date status.0
is there way acess last item of array without knowing lenght of array?
because following doesn't work:
name id date.-1.date status.-1
any idea of correct notation? or if it's not possible?
it's not possible reference last element of array without knowing length of array, since notation array_field.index
index in [0, length - 1]. use aggregation framework create view of data want export, save temporarily collection $out, , mongoexport that. example, documents do
db.collection.aggregate([ { "$unwind" : "$date" }, { "$group" : { "_id" : "$_id", "date" : { "$last" : "$date" } } }, { "$out" : "temp-for-csv" } ])
in order last date each document , output collection temp-for-csv.
you can return last elements in array $slice
projection operator, isn't available in aggregation , mongoexport takes query specification, not projection specification, since --fields
, --fieldfile
option supposed suffice. might feature request ask using query projection mongoexport.
Comments
Post a Comment