0

I am currently thinking about how to structure data for a node.js app i will be developping.
I am thinking of using Mongo as database, but cannot think of a good way to accomplish what I want. All my researches led me to cross-documents references, but this is not what i am looking for, as everything lies in my 'event' document.

My document consists in an event in which people take part that can be represented like that :

{
  name: "Some event name",
  people: ["A", "B", "C", "D", ... ],  // Could be more complex objects, with name, mail for instance
  actions : [
    {
        name: "Some action name"
        someAttr: 4.21
        leader: "A"  // One of people
        followers : ["A", "B", "D"]  // from people too
    },
    ...
  ]
}

As you can see, the people are repeated in the document. They can be repeated numerous times as each event will gather lots of actions.

Should I keep my schema this way, or is there something more clever to do such as referencing people from the actions with the index of the global people list with something like that ?

{
  name: "Some event name",
  people: ["A", "B", "C", "D", ... ],
  actions : [
    {
        name: "Some action name"
        someAttr: 4.21
        leader: 0
        followers : [0, 1, 3]
    },
    ...
  ]
}

If so, what is the smartest way to accomplish that ?

Thank you very much

Note : This is a theoretical question, and I am a beginner in the Mongo and node.js world

d6bels
  • 1,432
  • 2
  • 18
  • 30

1 Answers1

2

The question "linking or embedding" has been discussed multiple times. A good start for your resaerch can be this question.

EDIT:

If (as you said) people are complex objects, than you should reference them by an id or a name. It makes no sense, to store the (complex) objects twice (or more) in the same document.

Community
  • 1
  • 1
heinob
  • 19,127
  • 5
  • 41
  • 61
  • Well, thanks, but still I don't see the point. I am probably misunderstanding it, but it seems to me that most of time, the "linking or embedding" is about splitting into different collection, which is not the case here. My "people" only have in meaning in the scope of my event, not in any other, I don't want to make a collection from them. The question is more like "repeating or referencing" data **in/from within the same entry**. – d6bels Feb 12 '14 at 13:16
  • Thanks ! So that makes the second way the "best" one, with the numbers in leader/followers being and id referencing the ones present in my `people` objets ? – d6bels Feb 12 '14 at 13:31
  • 2
    I would make `people` an array of ojects. Each object has an `id` property. This `id` can be used in `followers` array. It would be a bad pattern to use the item index of the array, because deleting the first person in people would force you to change the whole `follower` array then. – heinob Feb 12 '14 at 13:52
  • Yes I meant using an array by speaking of using the index, which would be indeed horrible. I think that this solves my question, thanks ! – d6bels Feb 12 '14 at 13:58