mongoexport aggregate export to a csv file

喜欢而已 提交于 2019-12-20 09:27:43

问题


I want to save the result from an aggregation into a csv file.

Using the mongo cmd line tool I can do this to get the results I want:

db.compras.aggregate({ $group : { _id : "$data.proponente", total : { $sum : "$price" } }}

How would I translate this into a mongoexport command that saves results into a csv?


回答1:


You can't run aggregate() queries through mongoexport. The mongoexport tool is intended for more basic data export with a query filter rather than full aggregation and data processing. You could easily write a short script using your favourite language driver for MongoDB, though.




回答2:


Slightly simpler option as of 2.6+ is to now add an $out step to your aggregate to put the results into a collection:

db.collection.aggregate( [ { aggregation steps... }, { $out : "results" } ] )

Then just use mongoexport

mongoexport -d database -c results -f field1,field2,etc --csv > results.csv

After that you might want to delete the temporary collection from the database, so that it does not keep using unnecessary resources, and also to avoid confusion later, when you have forgotten why this collection exists in your database.

db.results.drop()



回答3:


You can export to a CSV file with the following 3 steps:

  1. Assign your aggregation results to a variable (reference):

    var result = db.compras.aggregate({ $group : { _id : "$data.proponente", total : { $sum : "$price" } }}
    
  2. Insert a value of the variable to a new collection:

    db.bar.insert(result.result);
    
  3. In terminal (or command line) export this bar collection to a CSV file:

    mongoexport -d yourdbname -c bar -f _id,total --csv > results.csv
    

...and you're done :)




回答4:


If you don't want to store the results in a collection, you could also write directly to a CSV file from JavaScript using the print function. Save the following script to a file like exportCompras.js.

var cursor = db.compras.aggregate({ $group : 
  { _id : "$data.proponente", 
    total : { $sum : "$price" }
  }
});

if (cursor && cursor.hasNext()) {

  //header
  print('proponente,total');

  while ( cursor.hasNext() ) {
    var item = cursor.next();
    print('"' + item._id + '",' + item.total);
    //printjson(cursor.next()); -- or if you prefer the JSON
  }

}

From the command line, call

mongo server/collection exportCompras.js > comprasResults.csv --quiet




回答5:


Take the following and save it on the mongo server somewhere:

// Export to CSV function
DBCommandCursor.prototype.toCsv = function(deliminator, textQualifier) 
{
    var count = -1;
    var headers = [];
    var data = {};

    deliminator = deliminator == null ? ',' : deliminator;
    textQualifier = textQualifier == null ? '\"' : textQualifier;

    var cursor = this;

    while (cursor.hasNext()) {

        var array = new Array(cursor.next());

        count++;

        for (var index in array[0]) {
            if (headers.indexOf(index) == -1) {
                headers.push(index);
            }
        }

        for (var i = 0; i < array.length; i++) {
            for (var index in array[i]) {
                data[count + '_' + index] = array[i][index];
            }
        }
    }

    var line = '';

    for (var index in headers) {
        line += textQualifier + headers[index] + textQualifier + deliminator;
    }

    line = line.slice(0, -1);
    print(line);

    for (var i = 0; i < count + 1; i++) {

        var line = '';
        var cell = '';
        for (var j = 0; j < headers.length; j++) {
            cell = data[i + '_' + headers[j]];
            if (cell == undefined) cell = '';
            line += textQualifier + cell + textQualifier + deliminator;
        }

        line = line.slice(0, -1);
        print(line);
    }
}

Then you can run the following commands via the shell or a GUI like Robomongo:

load('C:\\path\\to\\your\\saved\\js\\file')
db.compras.aggregate({ $group : { _id : "$data.proponente", total : { $sum : "$price" } }}.toCsv();



回答6:


By the same logic, in my case, I want the output as JSON data into out.json

1- Create file.js on local, not on the server

date = new Date("2019-09-01");
query = {x: true, 'created': {$gte: date}};
now = new Date();
userQuery = {'user.email': {$nin: [/\.dev$|@test\.com$|@testing\.com$/]}};
data = db.companies.aggregate([
    {$match: query},
    {$sort: {x: 1, created: 1}},
    {$lookup: {as: 'user', from: 'users', localField: 'creator', foreignField: '_id'}},
    {$addFields: {user: {$arrayElemAt: ['$user', 0]}}},
    {$match: userQuery},
    {$project: {"_id": 1, "name": 1, "user.email": 1, "xyz": 1}}
]).toArray();

print(JSON.stringify(data));

2- Run

mongo server/collection file.js > out.json --quiet

3- Delete header and check out.json

[
  {
    "_id": "124564789",
    "xyz": [
      {
        "name": "x",
        "value": "1"
      },
      {
        "name": "y",
        "value": "2"
      }
    ],
    "name": "LOL",
    "user": {
      "email": "LOL@LOL.com"
    }
  },
....
....
}]


来源:https://stackoverflow.com/questions/16468602/mongoexport-aggregate-export-to-a-csv-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!