0

Query Output:

"result" : [
            {
                "total" : 149,
                "email" : "TEST8@GMAIL.COM"
            },
            {
                "total" : 54,
                "email" : "TEST7@GMAIL.COM"
            } ...
        ],
        "ok" : 1

Query: Aggregate Query in Mongodb returns specific field

Now I am trying to export the query output to csv file. I want to get output file as shown below:

total(column 1)  Email(column 2)
149              TEST8@GMAIL.COM
54               TEST7@GMAIL.COM
... 

Please help!

Community
  • 1
  • 1
Anubhav
  • 1,605
  • 4
  • 18
  • 31
  • The answer to this question might help. [http://stackoverflow.com/questions/8971151/file-write-operations-in-mongo-script] – Rhod Pumaras Aug 21 '14 at 08:52
  • I tried this already, Giveing me this response Thu Aug 21 14:35:31.844 TypeError: Object [object Object] has no method 'hasNext' – Anubhav Aug 21 '14 at 09:06

1 Answers1

0

You could basically store the results into a different (temporary) collection and then generate csv using that collection. This could be done by adding $out field in your aggregation pipeline.

There are good examples and explanation here: http://docs.mongodb.org/manual/reference/operator/aggregation/out/

-- From the link

$out

Takes the documents returned by the aggregation pipeline and writes them to a specified collection. The $out operator must be the last stage in the pipeline. The $out operator lets the aggregation framework return result sets of any size.
Lalit Agarwal
  • 2,354
  • 1
  • 14
  • 18
  • You started on the right note. Further a simple mongoexport on this collection will yield the required result. – vmr Aug 21 '14 at 08:54