mings
mings

Reputation: 31

Using for loop with async in nodejs

I have connected with mongodb and nodejs.

I have question about sav query results to list.

results = [];

for(i = 0; i<query.length; i++){
    db.collection(collectionName).find(query[i]).toArray(function(err, result){
        results[i] = result;
    });
}

I used async.waterfall but it doesn't work very well... If you fixed this problem like me, would you please how to fix this?

query like this :

{
location:{
  $geoWithin : {
    $center: [[lng, lat], radian]}}, 
time : "time value" 
}

lng, lat, time are list..

Upvotes: 2

Views: 78

Answers (1)

chridam
chridam

Reputation: 103475

Depending on what the query array holds, you are better off running a single query which uses the $or operator instead of looping through the query array and firing server requests for each query:

db.collection(collectionName).find({ '$or': query }).toArray((err, results) => {
    console.log(results);
});

Upvotes: 1

Related Questions