如何将嵌套数组数据从mongodb导出到csv

时间:2018-02-12 12:42:09

标签: arrays mongodb csv

我在mongodb的集合中有以下json格式的文档。现在我需要将这些数据导出到仅包含指定字段的csv中,我不想要 frequency_serving" Never" csv中的值对象(在VFoodDetails数组下)。我尝试了下面的命令

-c FrequencyQuestionForm --type=csv --fields data.formList.IdentificationDetails.Group_Id,data.formList.IdentificationDetails.Study_Name -q '{"data.formList.IdentificationDetails.Study_Id":"1"}' --out D:/MongoDBReports/Book1.csv

它会抛出这样的错误

  

"位置参数太多:[dbmongo 27017 FormDataBase   FrequencyQuestionForm' data.formList.0.IdentificationDetails.0.Group_   ID,data.formList.0.IdentificationDetails.0.Study_Name']   2018-02-12T17:37:58.012 + 0530尝试mongoexport --help'更多   信息"

[
      {
        "_id": "5a6026e29cbcdc48083dae1a",
        "data": {
          "formList": [
            {
              "IdentificationDetails": [
                {
                  "Status": "Data Entry Completed",
                  "Form_Id": 1,
                  "Interviewer_Name": "NAGARAJ.P",
                  "Group_Id": 9,
                  "Study_Id": "1",
                  "Study_Name": "INDIAB",
                  "Created_Emp_Id": "1",
                  "Form_Name": "Food Frequency Questionnarie",
                  "Visit_No": "1",
                  "Volunteer_Id": "R270116",
                  "Interview_Date": "2009-10-10T00:00:00Z",
                  "Volunteer_Name": "RAJENDRA PAWAR"
                }
              ]
            },
            {
              "VFoodDetails": [
                {
                  "Portion_Size": 15,
                  "Serving_Size": "0",
                  "Food_Id": 737,
                  "Value": "0",
                  "Portion_Tool": 18,
                  "Volunteer_Id": "R270116",
                  "Meal_Session": 121,
                  "Frequency_Serving": "Daily"
                },
                {
                  "Portion_Size": 168,
                  "Serving_Size": "0",
                  "Food_Id": 740,
                  "Value": "0",
                  "Portion_Tool": 177,
                  "Volunteer_Id": "R270116",
                  "Meal_Session": 121,
                  "Frequency_Serving": "Never"
                }
              ]
            }
          ]
        }
      },
      {
        "_id": "5a6026e29cbcdc46783dae1a",
        "data": {
          "formList": [
            {
              "IdentificationDetails": [
                {
                  "Status": "Data Entry Completed",
                  "Form_Id": 1,
                  "Interviewer_Name": "NAGARAJ.P",
                  "Group_Id": 9,
                  "Study_Id": "1",
                  "Study_Name": "INDIAB",
                  "Created_Emp_Id": "1",
                  "Form_Name": "Food Frequency Questionnarie",
                  "Visit_No": "1",
                  "Volunteer_Id": "R270176",
                  "Interview_Date": "2009-10-10T00:00:00Z",
                  "Volunteer_Name": "KUMARAN PAWAR"
                }
              ]
            },
            {
              "VFoodDetails": [
                {
                  "Portion_Size": 15,
                  "Serving_Size": "0",
                  "Food_Id": 737,
                  "Value": "0",
                  "Portion_Tool": 18,
                  "Volunteer_Id": "R270176",
                  "Meal_Session": 121,
                  "Frequency_Serving": "Never"
                },
                {
                  "Portion_Size": 168,
                  "Serving_Size": "0",
                  "Food_Id": 740,
                  "Value": "0",
                  "Portion_Tool": 177,
                  "Volunteer_Id": "R270176",
                  "Meal_Session": 121,
                  "Frequency_Serving": "Weekly"
                }
              ]
            }
          ]
        }
      }
    ]

1 个答案:

答案 0 :(得分:1)

尝试使用双引号或单引号围绕--fields。我还认为你需要在字段export中指定第一个数组元素。

使用您提供的文件:

 mongoexport --host localhost --port 40001 -d test -c foo  --type=csv --fields "data.formList.0.IdentificationDetails.0.Group_Id,data.formList.0.IdentificationDetails.0.Study_Name" --out Book1.csv

输出:

2018-02-13T19:32:50.172+0000    connected to: localhost:40001
2018-02-13T19:32:50.173+0000    exported 2 records
[mongodb@localhost ~]$ cat Book1.csv

data.formList.0.IdentificationDetails.0.Group_Id,data.formList.0.IdentificationDetails.0.Study_Name
9,INDIAB
9,INDIAB