简体   繁体   中英

MongoDB aggregate result in the table format not in array

Hi I have used aggregation in Mongo similar to the one shown in the below link MongoDB Return the count of documents for each day for the last one month

I am getting the output as

/* 0 */    
{
    "result" : [ 
        {
            "_id" : "2014-03-17",   
            "count" : 1
        },   
        {
            "_id" : "2014-03-15",  
            "count" : 9
        }        
    ],  
    "ok" : 1  
} 

I need the result to be displayed in a table format so that I want to save this two columns in an excel sheet for future reference..

Mongodb is a document oriented data base, so you need to write a small code to do the job for you. Or google to get the code one guy already write like the one on the question How to convert JSON to CSV format and store in a variable and adapt it to your case.

The result of the aggregate command is an array, and cannot currently be returned otherwise. In JavaScript, you iterate an array to do what you want with the underlying result in a manner similar to this:

var data = db.collection.aggregate([ /* your aggregation */ ]);

data.result.forEach(function(doc) {

    // do something with `doc`

});

Other languages will have similar handling or possibly even another type to handle the aggregate result.

In future releases, the results of aggregate will be a cursor which you iterate just like a standard cursor from find.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM