0

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'}]
         }]
)
3
  • Mongo is a "document" based database...this means it basically just takes JSON objects as its input data...so yes...you can definitely nest data with JSON objects. Commented Jun 30, 2014 at 20:23
  • example JSON object with arrays: var object = {"array": yourarray, "array2": yourarray2} Commented Jun 30, 2014 at 20:26
  • json.org/example - here is a better example. Commented Jun 30, 2014 at 20:27

1 Answer 1

1

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/

Sign up to request clarification or add additional context in comments.

3 Comments

I see, so i must use arrays that way, i thought about that, but didn't know if i could do the way i wanted to, thanks man, i wish i could vote for your answer.
@user3529913 Edited my answer to make it more clear what the issue is with your example.
So, it'd be like this db.siteraiz.insert( SiteRaiz:[ Dados:'idSiteRaiz:#ChartSet', [{Metas:'metaValor'}], [{Robots:'link1:#linkN'} ] ] }) i wanted to put meta and robots inside siterais, but what you did (the last one) it's what I want, really thanks man.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.