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

173
Views
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 answers
Answer question

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 Report
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!