This is a very simplified version of my query. Is it acceptable to join the two collections for the client with the use of for loops?
There could be up to 2,000 books, which also means there could be an equal number of favorite books per user in the favoriteBooksCollection
.
return favoriteBooksCollection
.find({user: current_userId})
.then(function(favorites){
var bookIds = favorites.map(function (a) {
return a.bookId
};
return BooksCollection
.find({_id : {$in: bookIds}})
.sort({timestamp: -1})
.then(function(books){
for(var i = 0; i < books.length; i++){
for(var j = 0; j < favorites.length; j++){
if(favorites[j].bookId == books[i]._id){
//'join' user's myRating to book document here
books[i].userRating = favorites[j].myRating;
};
};
};
// return books array of objects with newly added UserRating property
return books;
});
});