I'm not really sure how to ask this question but let me explain: Let's say I have an app where users play different mini games that get logged to their account. Users and Games are stored in a database like so:

const userSchema = new mongoose.Schema({
   username: 'String',
   password: 'String',
   games: [{type: 'ObjectId'}]

const gameSchema = new mongoose.Schema({
   name: 'String',
   startTime: 'Date',
   endTime: 'Date'

I.e. games are associated with user profiles via an array of ObjectIds.

Now lets say I want to retrieve user information and their detailed list of games played for a profile page using only their userId. I could get this information from two existing endpoints:

Starting with

GET myapi.com/users/{userId}

Then taking the array of games from the response on the client and using it to make a second request:

GET myapi.com/games?ids=[id1, id2, id3, etc.]

However, this seems poorly optimized since I have to make two separate requests. I assume it would be faster to make an endpoint that takes the userId and and performs the function of both the above endpoints in one request:


So I would get an object containing the user's info and the info of the games they've played. The obvious downside of this is that this endpoint would probably only serve one purpose.

This is a very small example, so I can't imagine the performance vary much either way, but for more resource intensive uses...maybe I have to chain 10+ queries... which method is better practice?


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.