2

I have a script which suppose to pull data from mongo and save data in JSON, CSV or EXCELL sheet. Currently, i am able to get data but during saving i am using the following script.

   import fs from "fs"
    
    const cursor = db.getCollection("users").find({}, { name: 1,  email:1 , 
        phone:1});
    
    let users = [];
    
    while (cursor.hasNext()) {
      const user = cursor.next();
      users.push({name: user.name, email:user.email, phone:user.phone})
     
    }
    
    //print(JSON.stringify(users))
    
    fs.writeFile ("test.json", JSON.stringify(users), function(err) {
        if (err) throw err;
        console.log('complete');
        }
    );

gives me this error

uncaught exception: SyntaxError: import declarations may only appear at the top level of a module :

I have tried a couple of solutions from here link1 link2

but non of them are working. Can anyone suggest me a solution

Ron Weasley
  • 223
  • 2
  • 10
  • did you figure this out? I'm trying to do the same thing, getting same error – cDub Nov 30 '22 at 23:17
  • 1
    Hi, though I didn’t get what I wanted i did a workaround since I needed the output of that query so ran the query on terminal and passed those result in file eg: mongo db_name --quiet --eval 'DBQuery.shellBatchSize = 2000; db.users.find({}).limit(2000).toArray()' > users.json – Ron Weasley Dec 01 '22 at 05:12

0 Answers0