Best strategy to add Array to Document?

I need to add an array of links to some documents—and I’m not sure how to approach this. Some document might have zero links, others might have up to two or even more links. Each link will have three values: ‘label’, ‘title’ and ‘url’. Is any of the following approaches preferable or is there a better way? Thanks!

// links with property and values
{"linkswithproperties":[
  {
    "linklabel": "twitter",
    "linktitle": "eboyarts",
    "linkURL": "https://twitter.com/eBoyArts"
  },
  {
    "linklabel": "database",
    "linktitle": "eboy.io",
    "linkURL": "http://eboy.io/"
  },
]}

alternative(?):

// link values only
{"linkvaluesonly":[
  ["twitter", "eboyarts", "https://twitter.com/eBoyArts"],
  ["database", "eboy.io", "http://eboy.io/"],
]}

The alternative would be a nightmare. Your first option isn’t bad, but it looks like you need to look into adding another collection instead of nesting. This would make it a lot easier.

1 Like

I would go for the first one but a little shorter:

{
  "links": [{
    "label": "twitter",
    "title": "foo",
    "url": "https://twitter.com/foo"
  }]
}

But like @tdhz77 said: Depending on the queries and updates you need to do, a separate collection might be easier.

1 Like

I think I’d go with a separate collection as well. Use the id of the parent to tie your “children” links to the “parent” document. And just to echo what @tdhz77 said: the alternative is a nightmare, one I personally experienced a long time ago!

1 Like

Thanks guys! I had not seriously considered a separate collection. I think I will go that route.

good array concep
{
“links”: [{
“label”: “twitter”,
“title”: “foo”,
“url”: “https://twitter.com/foo
}]
}