Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

171
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda