1

I have the below json format of document in my collection in mongodb.Now i need to export this data into csv with only specified fields and i dont want frequency_serving "Never" value object(Under VFoodDetails Array)in csv.What i tried the below command

-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

it throws an error like this

"too many positional arguments: [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 try 'mongoexport --help' for more information"

[
      {
        "_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 Answer 1

1

Try surrounding the --fields with either double or single quotes. I also think you need to specify the first array element in the fields export.

Using the documents you’ve provided:

 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

Output:

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
Sign up to request clarification or add additional context in comments.

4 Comments

Sorry, its not working. i am getting the same error.
@srikasi what version of mongo are you running? Please see my updated answer, it’s working on my dev machine running v3.6 on Linux
Thanks N3i1,it is working fine but what i want in my output is: data.formList.0.IdentificationDetails.0.Group_Id,data.formList.0.IdentificationDetails.0.Study_Name,VFoodDetails.Portion_Size,VFoodDetails.Portion_Size,Serving_Size, VFoodDetails.Frequency_Serving 9,INDIAB,15,0,Never 9,INDIAB,168,0,Weekly
@srikasi My answer is based on helping you solve the error you were receiving regarding: "too many positional arguments", which it has!!. How to display your data in the way you wish is another Q&A

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.