0

Inspired by this question in Server Fault https://serverfault.com/questions/459042/mongoexport-csv-output-array-values

I'm using mongoexport to export some collections into CSV files, however when I try to target fields which are the last members of an array I cannot get it to export correctly.

Command I'm using

mongoexport -d db -c collection -fieldFile fields.txt --csv > out.csv

One item of my collection:

{
    "id": 1,
    "name": "example",
    "date": [
        {"date": ""},
        {"date": ""},
    ],
    "status": [
         "true",
         "false",
    ],
}

I can access to the first member of my array writing the fields like the following

name
id
date.0.date
status.0

Is there a way to acess the last item of my array without knowing the lenght of the array?

Because the following doesn't work:

name
id
date.-1.date
status.-1

Any idea of the correct notation? Or if it's simply not possible?

1 Answer 1

1

It's not possible to reference the last element of the array without knowing the length of the array, since the notation is array_field.index where the index is in [0, length - 1]. You could use the aggregation framework to create the view of the data that you want to export, save it temporarily into a collection with $out, and then mongoexport that. For example, for your documents you could do

db.collection.aggregate([
    { "$unwind" : "$date" },
    { "$group" : { "_id" : "$_id", "date" : { "$last" : "$date" } } },
    { "$out" : "temp-for-csv" }
])

in order to get just the last date for each document and output it to the collection temp-for-csv.

You can return just the last elements in an array with the $slice projection operator, but this isn't available in aggregation and mongoexport only takes a query specification, not a projection specification, since the --fields and --fieldFile option are supposed to suffice. Might be a good feature request to ask for using a query with a projection for mongoexport.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for this answer yep that might be a good fetature to request because I wanted to avoid scripting more juste using the fields should be usefull by itself

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.