2

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..

7
  • why don't you use async-await? Commented Jul 12, 2018 at 9:21
  • What do you mean, "doesn't work very well"? What is unclear to you and needs to be clarified? Commented Jul 12, 2018 at 9:21
  • I do not understand "async" yet. I will look into asyn-await.. Commented Jul 12, 2018 at 9:27
  • wait let me write an answer for you. Commented Jul 12, 2018 at 9:33
  • As a side node: For the most cases, querying database in a loop is not a good idea. Commented Jul 12, 2018 at 9:36

1 Answer 1

1

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);
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.