I’m trying to build a platform where each user is able to add games to their library, under either the “owned” or “wanted” categories, and then it is the platform’s responsibility to find possible trade options.
For example, if user A has game A in their owned list, and user B wants game A, and owns games C and D, then user A will see games C and D under their possible trade options, in return for game A.
Currently, I’ve implemented this somewhat crudely using too many nested DB queries and I feel like there’s a faster way to do this.
The following is what happens every time a user wants to see all of their trade options.
let foundUser = await User.findById({_id: req.session.user_id})
let gamesList = []
//for each game in the users have library, search it up in the game DB and add wantedBy users to an array
for (let i = 0; i < foundUser.library.have.length; i++){
let foundGame = await Game.findById({_id: foundUser.library.have[i].gameID})
//for this game, add each user in the WantedBy List to a local array
for (let j = 0; j < foundGame.wantedBy.length; j++){
//for each of these users, use their have list to build the trade object
let newFoundUser = await User.findById({_id: foundGame.wantedBy[j]})
//check if the other user wants the game for the same platform as foundUser.library.have[i].platform
console.log(newFoundUser)
console.log(foundGame.wantedBy)
let platformMatch = newFoundUser.library.want.find(game => (game.platform === foundUser.library.have[i].platform) && (game.gameID == foundUser.library.have[i].gameID))
if (platformMatch){
for (let k = 0; k < newFoundUser.library.have.length; k++){
//for each of these games in the other users have list, make a trade object
let newFoundGame = await Game.findById({_id: newFoundUser.library.have[k].gameID})
let tradeObject = {
gameOffered: newFoundUser.library.have[k].gameID,
gameOfferedBackImg: newFoundGame.backImg,
gameOfferedPlatform: newFoundUser.library.have[k].platform,
gameOfferedTitle: newFoundGame.title,
inReturnForTitle: foundGame.title,
inReturnForPlatform: foundUser.library.have[i].platform,
inReturnFor: foundGame._id,
otherUserID: newFoundUser._id
}
gamesList.push(tradeObject)
}
}
}
}
res.render('trade', {gamesList: gamesList, mainUser: foundUser._id})
The nested loops and DB queries make this really slow, especially if the no. of users and the no. games in their library increases.
The following are the DB schemas:
The User Schema:
username: {
type: String,
required: ['true', 'Username cannot be blank'],
},
email: {
type: String,
required: ['true', 'Email cannot be blank']
},
password: {
type: String,
required: ['true', 'Password cannot be blank'],
},
phoneNum: {
type: String,
required: true,
unique: true
},
isVerified: false,
address: {
city: String,
area: String,
street: String,
nearestLandmark: String
},
verificationCode: String,
library: {
have: [{
gameID: String,
platform: String
}],
want: [{
gameID: String,
platform: String
}]
},
notifications: [
{
dateTime: Date,
message: String
}
]
})
The Game Schema:
const gameSchema = new mongoose.Schema({
title: String,
backImg: String,
wantedBy: [String]
})
Any help optimising this would be greatly appreciated. Thank you!