Monday 13 January 2020

Export MongoDB to CSV using Node.js but loosing some column

I want to export table to csv from mongodb using this script:

const mongodb = require("mongodb").MongoClient;
const fastcsv = require("fast-csv");
const fs = require("fs");
let url = "mongodb://...."

mongodb.connect(
  url,
  { useNewUrlParser: true, useUnifiedTopology: true },
  (err, client) => {
    if (err) throw err;

    client
      .db("mydb")
      .collection("mycsv")
      .find({})
      .toArray((err, data) => {
        if (err) throw err;

        console.log(data);
        fastcsv
          .write(data, { headers: true })
          .on("finish", function() {
            console.log("Write to mycsv.csv successfully!");
          })
          .pipe(fs.createWriteStream("mycsv.csv"));

        client.close();
      });
  }
);

But I lost some column, what I know is the column that loses is the column with many null values (first 100 rows value of that columns is null).



from Export MongoDB to CSV using Node.js but loosing some column

No comments:

Post a Comment