Mongodb

mongoexport csv 輸出數組值

  • June 4, 2014

我正在使用 mongoexport 將一些集合導出到 CSV 文件中,但是當我嘗試定位作為數組成員的欄位時,我無法正確導出它。

我正在使用的命令:

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

fields.txt 的內容類似於

id
name
address[0].line1
address[0].line2
address[0].city
address[0].country
address[0].postcode

BSON 數據在哪裡:

{
   "id": 1,
   "name": "example",
   "address": [
       {
           "line1": "flat 123",
           "line2": "123 Fake St.",
           "city": "London",
           "country": "England",
           "postcode": "N1 1AA"
       }
   ]
}

導出數組內容的正確語法是什麼?

你幾乎是對的,試試這個:

id
name
address.0.line1
address.0.line2
address.0.city
address.0.country
address.0.postcode

我將您的範例文件插入到bar數據庫中test的集合中,然後像這樣執行導出:

./mongoexport --port 31000 -d test -c bar -fieldFile fields.txt --csv > out.csv

然後檢查了結果,這對我來說看起來不錯:

cat out.csv
id,name,address.0.line1,address.0.line2,address.0.city,address.0.country,address.0.postcode
1.0,"example","flat 123","123 Fake St.","London","England","N1 1AA"

引用自:https://serverfault.com/questions/459042