Dove
Dove

Reputation: 125

MongoDB collection scheme

I am starting to develop online football management game using NodeJS and MongoDB. But now i don't know, should i use multiple collections or can i put everything in one ? Example:

{
  "_id" : ObjectId("5118ee01032016dc02000001"),
  "country" : "Aruba",
  "date" : "February 11th 2013, 3:11:29 pm",
  "email" : "[email protected]",
  "name" : "test",
  "pass" : "9WcFwIITRp0e82ca3c3b314a656bfb437553b1d013",
  "team" : {
    "name" : "teamname",
    "logo" : "urltologo",
    "color" : "color",
    "players" : [{
        "name" : "name",
        "surname" : "surname",
        "tackling" : 58,
        "finishing" : 84,
        "pace" : 51,
         ....
      }, {
        "name" : "name",
        "surname" : "surname",
        "start_age" : 19,
        "tackling" : 58,
        "finishing" : 84,
        "pace" : 51,
         ...
      }],
    "stadium" : {
        "name" : "stadium",
        "capacity" : 50000,
         "pic" : "http://urltopic",
        ....
     },
  },
}

or create different collections for users, fixtures, players, teams ? Or any other method ?

Upvotes: 0

Views: 796

Answers (2)

Hacknightly
Hacknightly

Reputation: 5164

When I started with MongoDB, I went by the mantra of 'embed everything', which is exactly what you're doing above. However, there needs to be some consideration for sub-documents that can grow to be very large. You should think about how often you'll be updating a particular document or subdocument as well. For instance, your players are probably going to be updated on a regular basis, so you'd probably want to put them in their own collection for ease of use. Anyway, the flexibility of MongoDB makes it so that there's really no 'right' answer to this problem, but it may help you to refer to the docs on data modeling.

Upvotes: 1

shargors
shargors

Reputation: 2157

There is no hard and fast rule on how to design schemas in mongo. A lot depends on your application data access patterns, frequency of data access and the relationships between different entities, how they shrink/grow/change and which of them stay intact. It is not feasible to give an advice without knowing how your application is supposed to work. I recommend you consult a book, such as MongoDB in Action for example, which has advice on how to design schema in mongo properly taking into the account application specific requirements.

Upvotes: 1

Related Questions