0

sample json data file

{
  "Includes": {
    "Employees": {
      "14": {
        "name": "john",
        "age": 12,
        "activity": {
          "Count": 3502,
          "RatingValue": 5
        }
      },
      "17": {
        "name": "smith",
        "age": 23,
        "activity": {
          "Count": 232,
          "RatingValue": 5
        }
      }
    }
  }
}

js was written to retrieve the nested document and stored in array

var result = [];

db.details.find().forEach(function(doc) {
    var Employees = doc.Includes.Employees;
    if (Employees) {
        for (var key in Employees) {
            var Employee = Employees[key];
            var item = [];
            item.push(key);
            item.push(Employee.name);
            item.push(Employee.age);
            item.push(Employee.activity.Count);
            item.push(Employee.activity.RatingValue);
            result.push(item.join(","));
        }
    }
});

print(result);

How can we store the output of array in csv with 2 rows because the data contains 2 rows by using mongoexport. In csv output must be

14,john,12,3502,5

17,smith,23,232,5

4
  • It works fine, you just have a typo: var Employees = doc.Includes.Empoyees;. Empoyees -> Employees Commented Oct 10, 2014 at 19:31
  • It was a mistake. Getting the output, dont know how to store in csv Commented Oct 10, 2014 at 19:33
  • How do you want to store it? You need to copy-paste the result or something Commented Oct 10, 2014 at 19:34
  • 14,john,12,3502,5 17,smith,23,232,5 it must be in individial columns i.e 14 in 1st column of 1st row,john in 2nd column so on.. 17 in 1st column of 2nd row.. Commented Oct 10, 2014 at 19:36

2 Answers 2

0
var csv=""; //this is the output file
for(var i=0;i<result.length;i++){//loop through output
    csv+=result[i]+"\n"; //append the text and a newline
}
window.open('data:text/csv;' + (window.btoa?'base64,'+btoa(csv):csv)); //open in a new window, chrome will automatically download since it is a csv.
Sign up to request clarification or add additional context in comments.

4 Comments

Getting a error : Reference error window is not defined
Are you not using a browser?
Might be useful to mention in question. Remove the last line from my code and print the csv variable or save it to file somehow.
You can just print/console.log csv and then save the output from the cli.
0

Change that final print(result); to the following:

print(result.join("\n"));

Then call your script and direct the output to a CSV file like so:

mongo --quiet "full-path-to-script.js" > "full-path-to-output.csv"

Note: The --quiet arg suppresses the standard Mongo header output (shell version and initial database).

I created a details collection, and added your JSON document to it, and then running the modified script resulted in the following CSV file content:

14,john,12,3502,5
17,smith,23,232,5

If you want a CSV header row as well, see my nearly identical answer to your nearly identical question here: https://stackoverflow.com/a/26310323/3212415

Comments

Your Answer

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