如何使用mongoexport仅导出子文档中的特定字段

Kho*_*hon 13 json mongodb

有没有办法在使用mongoexport时只导出子文档中的指定字段?mongo docs说只使用-f field1,field2等......但这只适用于顶级字段.我在主文档中有一个文档,也有字段.有没有办法只获得那些?

例:

{
    "topField1": "topValue1",
    "topField2": "topValue2",
    "subDoc1: {
                  "subField1": "subValue1",
                  "subField2": "subValue2"
              }
}
Run Code Online (Sandbox Code Playgroud)

有没有办法指定我只获得字段subField2?

我知道在常规的mongo查询中,我可以使用"subDoc1.subField2",这很简单, return {"$oid": 122432432, {"subDoc1":{"subField2": "subValue2"}}但这似乎不适用mongoexport.

我也想出口json.

小智 16

你使用dotnotation会得到什么样的错误?我正在运行mongoDB 1.8.2,对我来说有以下工作:

mongoexport -d dbName -c collectionName -f subDoc1.subField2 --csv -o /path/to/file.csv
Run Code Online (Sandbox Code Playgroud)

CSV看起来像这样

subDoc1.subField2 #header with field names
"subValue2" #actual entry
Run Code Online (Sandbox Code Playgroud)