Search code examples
bashmongodbcsvmongoexport

Mongoexport --fields option for an array element


I have a bash script for generating daily csv reports from a collection. In the script I'm using mongoexport such as:

mongoexport --authenticationDatabase $DBNAME -h $CONN_STRING -u $USERNAME -p $PASSWORD --db listings --collection listings --type=csv --out $EXTRACTED_FILE_NAME --fields _id,Version,MerchantId,HBSku,MerchantSku,Price.Currency,Price.Amount,AvailableStock,CommissionRate,CommissionType,PaymentTermInDays,CreatedAt,UpdatedAt,IsActive,DispatchTime,IsSalable,CargoCompanies.0 --query "{ \$or: [{\"CreatedAt\" : { \$gte : { \"\$date\" : \"$OLD_DATE$DATE_POSIX\" }}}, {\"UpdatedAt\" : { \$gte : { \"\$date\" : \"$OLD_DATE$DATE_POSIX\" }}}]}"

Or problem related part:

--fields _id,Version,MerchantId,HBSku,MerchantSku,Price.Currency,Price.Amount,AvailableStock,CommissionRate,CommissionType,PaymentTermInDays,CreatedAt,UpdatedAt,IsActive,DispatchTime,IsSalable,CargoCompanies.0

The last parameter, CargoCompanies is an integer array and represented as:

"CargoCompanies" : [
    NumberInt(1)
]

MongoExport generates file and puts empty string to CargoCompanies.0 field.

What I want is that mongoexport should put first element value of CargoCompanies: 1. Not NumberInt(1), just 1.

What should I do?

Thanks.


Solution

  • In mongoexport's new version r3.0.14, the bug is fixed, When you try to export an array field of integers,

    [1,2,3] is printed in a csv file instead of

    [
        NumberInt(1), NumberInt(2), NumberInt(3)
    ]