My userlist table in mongo is set up like this:
email: [email protected], uniqueUrl:ABC, referredUrl: ...
I have the following code where I query all of the users in my database, and for each of those users, find out how many other's users' referredUrl's equal the current user's unique url:
exports.users = function(db) {
return function(req, res) {
db.collection('userlist').find().toArray(function (err, items) {
for(var i = 0; i<= items.length; i++) {
var user = items[i];
console.log(user.email);
db.collection('userlist').find({referredUrl:user.uniqueUrl}).count(function(err,count) {
console.log(count);
});
}
});
};
};
Right now I'm first logging the user's email, then the count associated with the user. So the console should look as such:
[email protected]
1
[email protected]
3
[email protected]
2
Instead, it looks like this:
[email protected]
[email protected]
[email protected]
1
3
2
What's going on? Why is the nested query only returning after the first query completes?