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

172
Vistas
Mongodb set variable to find().toarray()

I'm having trouble getting the following to work

var data = db.collection('mycollection', function(er, collection) {
                return collection.find().toArray();
           });

The resulting data is not an array. I don't get what's wrong. How do I set a varaible to the contents of find().toArray()?

I've tried logging the contents like this so I know that there must be data:

db.collection('mycollection', function(er, collection) {
   collection.find().toArray(function(err, results) {
       for (var i = 0; i < results.length; i++) {
           console.log(results[i]);
       }
   });
});

Thanks! I'm very new to ajax programming and mongodb.

about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Just go it like in simple way:-

.find method always provide data in array format.

var query = db.collection.find({},function(err,data){

if(err) throw err;

console.log(data); // give in array.
})

thanks

about 4 years ago · Santiago Trujillo Denunciar
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