How can I store the result of MongoDB query in a text file with Nodejs?

Alex Nguyen

I get and store the data from mongoDB into txt file by using the following code. But when I open txt file. Nothing is saved.

var express = require('express'); 
var router = express.Router(); 
var fs = require('fs');
var mongodb = require('mongodb').MongoClient;
mongodb.connect('mongodb://127.0.0.1:27017/data', function(err, db) {
  if (err) throw err;
  var test = db.collection('test');
  for (var i = 0; i < 10; i++)
  {
    test.find({ "number": i }, {"email": 1, "_id": 0}).toArray(function (err,data) {
      if (err) throw err;
                fs.writeFile("/tmp/test", data, function(err) {
            if(err) {
                return console.log(err);
            }

            console.log("The file was saved!");
        });
      //console.log(data);
  });
  }
});

It's result of console.log(data);

[ { email: '[email protected]' } ]
[ { email: '[email protected]' } ]
[]
[ { email: '[email protected]' } ]
.......

How can store result of MongoDB query in text file ?

rohith
mongodb.connect('mongodb://127.0.0.1:27017/data', function (err, db) {
if (err) throw err;
var test = db.collection('test');
recursiveadd(0,10,"",function(err,data){
    if (err) {
        return console.log(err);
    }else{
        fs.writeFile("/tmp/test", data, function (err) {
        if (err) {
            return console.log(err);
        }

        console.log("The file was saved!");
      });
    }
 })


});

function recursiveadd(i,n,datatofile,cb){
    if(i>n){
      cb(undefined,"success");
      return;
   }
    test.find({
        "number": i
    }, {
        "email": 1,
        "_id": 0
    }).toArray(function (err, data) {
        if (err)cb(err);
           datatofile = datatofile + " " + JSON.stringify(data);
    });
    recursiveadd(i++,n,datatofile,cb);

}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I store a mongodb query result in a variable?

From Dev

Can I store LINQ query result in an array?

From Dev

How can I store edited text file using bash?

From Dev

How can I store a very large text file into an array?

From Dev

How can I store the result of a function into a variable?

From Dev

How can I store the result of .each() into localStorage?

From Dev

How can I retrieve the query definition (SQL text) of an Access query and store back a changed definition

From Dev

How can I store a backgammon board in MongoDB?

From Dev

How can I store a backgammon board in MongoDB?

From Dev

PaperJS variables, how can I store these in MongoDB?

From Dev

How i can use result of subquery in this query?

From Dev

In MongoDB with Mongoose (NodeJS) how can I query(find) over a populated field?

From Dev

How can I store a knex query in a variable?

From Dev

How can I pair values together from one text file and output the result to a separate file using pandas?

From Dev

NodeJS - how can get mysql result with executed query?

From Dev

how can executorService store result of task `i` in `array[i]`?

From Dev

How to store a query result in a variable

From Dev

How can I store my variable in a file?

From Dev

how can i write this query with mongodb?

From Dev

Python - How can I store data from a text file without storing all data simultaneously in the primary memory?

From Dev

How can I query a MongoDB collection by both a geo spatial index and a text index quickly?

From Dev

How can I use a query result as a column for a separate query?

From Dev

How can I use a query result as a column for a separate query?

From Dev

How can I rename a file on the SFTP with NodeJs

From Dev

How can I rename a file on the SFTP with NodeJs

From Dev

How can I store reference to the result of an operation in Go?

From Dev

How can I store a result of a function in a variable in Python?

From Dev

How can i store the result of SRANDMEMBER with a COUNT argument in a SET?

From Dev

How can I store a result of a function in a variable in Python?

Related Related

  1. 1

    How do I store a mongodb query result in a variable?

  2. 2

    Can I store LINQ query result in an array?

  3. 3

    How can I store edited text file using bash?

  4. 4

    How can I store a very large text file into an array?

  5. 5

    How can I store the result of a function into a variable?

  6. 6

    How can I store the result of .each() into localStorage?

  7. 7

    How can I retrieve the query definition (SQL text) of an Access query and store back a changed definition

  8. 8

    How can I store a backgammon board in MongoDB?

  9. 9

    How can I store a backgammon board in MongoDB?

  10. 10

    PaperJS variables, how can I store these in MongoDB?

  11. 11

    How i can use result of subquery in this query?

  12. 12

    In MongoDB with Mongoose (NodeJS) how can I query(find) over a populated field?

  13. 13

    How can I store a knex query in a variable?

  14. 14

    How can I pair values together from one text file and output the result to a separate file using pandas?

  15. 15

    NodeJS - how can get mysql result with executed query?

  16. 16

    how can executorService store result of task `i` in `array[i]`?

  17. 17

    How to store a query result in a variable

  18. 18

    How can I store my variable in a file?

  19. 19

    how can i write this query with mongodb?

  20. 20

    Python - How can I store data from a text file without storing all data simultaneously in the primary memory?

  21. 21

    How can I query a MongoDB collection by both a geo spatial index and a text index quickly?

  22. 22

    How can I use a query result as a column for a separate query?

  23. 23

    How can I use a query result as a column for a separate query?

  24. 24

    How can I rename a file on the SFTP with NodeJs

  25. 25

    How can I rename a file on the SFTP with NodeJs

  26. 26

    How can I store reference to the result of an operation in Go?

  27. 27

    How can I store a result of a function in a variable in Python?

  28. 28

    How can i store the result of SRANDMEMBER with a COUNT argument in a SET?

  29. 29

    How can I store a result of a function in a variable in Python?

HotTag

Archive