Q:

Find with $in clause causing stackoverflow in Mongodb & Nodejs

I'm trying to fetch multiple entries using $in. I'm getting a stackoverflow in the callback even though I have only have 3 records in the db. My function looks like this -
 // users is an array
 // callback is function(err, result) 
 function(users, callback) {
      users = db.collection('users');
      users.find({ username: {$in: users }}, function(err, items) {
          // items is a cursor
          items.toArray(callback);
      });
 }

Can't seem to figure out what the issue is

A:

You need to give your users array parameter and the users collection variable different names:
function(userids, callback) {
    var users = db.collection('users');
    users.find({ username: {$in: userids }}, function(err, items) {
        // items is a cursor
        items.toArray(callback);
    });
}

As it is now, you're overwriting the users parameter array of ids with the users collection.

