juli1234
juli1234

Reputation: 97

Mongodb nested resources API

I'm really new to mongodb and I want to build something to practice.

Imagine I have a collection that looks like this

{
  "game": "stars",
  "players": [
    {
      "name": "Jhon Smith",
      "score": 10
    }
  ]
}

Just a simply collection where I stored the name of a game and the players who are actually playing that game.

So, my routes will look like this

GET /games <- retrieves all the games
GET /games/:id <- find a game by id 
GET /games/:id/players <- to get the players of a game

My questions are:

  1. is it a good practice to add that last route GET /games/:id/players in order to get the games' players? I'm asking this and another question comes to my head
  2. Does using mongodb changes the way we build an API?
  3. Is this a good practice or is there any other workaround?

Upvotes: 0

Views: 84

Answers (1)

MattM
MattM

Reputation: 1289

  1. Your routes are fine. There are probably many different opinions on the "best" way but I don't see any problem with the way you've done it.

  2. Nope, the choice of database probably shouldn't affect the design of your API. In fact, this is one of the benefits of making an API...the consumers of the API don't need to know the database details behind it.

  3. There are other options but I think the way you've done it is fine. Here's another option though if you'd like to see some other styles:

GET /players?gameId=123

Upvotes: 1

Related Questions