2011-07-14 19 views
10

有沒有辦法在使用mongoexport時僅導出子文檔中的指定字段? mongo文檔說只使用-f field1,field2等...但只適用於頂級域。我在主文檔裏面還有一個文檔,裏面也有字段。有沒有辦法只得到那些?如何使用mongoexport僅導出子文檔中的特定字段

例子:

{ 
    "topField1": "topValue1", 
    "topField2": "topValue2", 
    "subDoc1: { 
        "subField1": "subValue1", 
        "subField2": "subValue2" 
       } 
} 

有指定我只得到外地subField2的方法嗎?

我知道在一個正常的mongo查詢中,我可以使用「subDoc1.subField2」,它將簡單地返回{「$ oid」:122432432,{「subDoc1」:{「subField2」:「subValue2」}}但這不會' t似乎與mongoexport一起工作。

我也想導出爲json。

回答

13

你使用dotnotation會出現什麼樣的錯誤?我運行的MongoDB 1.8.2和我下面的作品:

mongoexport -d dbName -c collectionName -f subDoc1.subField2 --csv -o /path/to/file.csv 

的CSV看起來像這樣

subDoc1.subField2 #header with field names 
"subValue2" #actual entry 
+1

我不認爲我得到了一個錯誤。我認爲問題在於它返回了不必要的字段。但你是對的,它現在可以工作!我想我以前正在運行mongo 1.6。現在我正在運行mongo 2.0.1。謝謝黛米! – Khon

0

在情況下,我們不知道在這種情況下,子文檔值.IE的: subField1subField2,但我們需要的僅僅是提取子集合的第一個字段。 mongoexport可以處理嗎?

0

mongoexport --db DB_NAME --collection COLLECTION_NAME --fields '不,名稱' --out collection_name.json

相關問題