Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

155
Views
Saving mongo data to csv,json,excel etc with js script

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

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!