Reno
Reno

Reputation: 2819

Mongodb with nested Arrays

I looked on the web and didn't find what I wanted.

I want to know if it's possible to insert an array inside another one like

Let's say I want to do this:

db.siteraiz.insert(     
    SiteRaiz:[
        [{Dados:'idSiteRaiz:#ChartSet',
         [{Metas:'metaValor'}],
         [{Robots:'link1:#linkN'}]
         }]
)

Upvotes: 0

Views: 63

Answers (1)

Gergo Erdosi
Gergo Erdosi

Reputation: 42028

Yes, it's possible to insert an array into another, but you can't use arrays in objects without a key. This syntax is invalid:

{
  Dados: 'idSiteRaiz:#ChartSet',
  [
    {Metas:'metaValor'}
  ],
  [
    {Robots:'link1:#linkN'}
  ]
}

You can use objects only with key-value pairs:

{
  key1: 'value1',
  key2: 'value2'
}

You can have arrays inside objects, but you still need to use a key for the array:

{
  key1: 'value1',
  key2: ['value2', 'value3']
}

I don't exactly understand how you want to structure your data, but here are some working examples:

db.siteraiz.insert({
  SiteRaiz:[
    {
      Dados: 'idSiteRaiz:#ChartSet'
    }
  ]
})
db.siteraiz.insert({
  SiteRaiz:[
    [
      {Metas:'metaValor'}
    ],
    [
      {Robots:'link1:#linkN'}
    ]
  ]
})

Make sure you are trying to insert a valid JSON object. You can validate your JSON object for example here: http://jsonlint.com/

Upvotes: 1

Related Questions