Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

158
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda