Consider the following simple schema:
var PostSchema = new Post({
body : String,
tags : []
}
Via a form POST (REST HTTP API that I'm developing), I would like to insert key/value pairs into the tags
array so that the resulting mongodb document would look like:
{
"body" : "This is the post body",
"tags" : [
{"Color" : "Orange"},
{"Color" : "Blue"},
{"Person" : "Ted"},
{"Person" : "Fred"},
{"Person" : "Joe"},
{"Stone" : "Diamond"}
]
}
My question is, how do I name the form fields for tags
to accomplish this? I was thinking something like tags[0][color]
for each color, but that doesn't work, that creates a separate array for each key inside the tags array with a list of the values.
I kind of feel silly now. The solution is to (obviously) supply the tags
like so:
tags[0][color] = orange
tags[1][color] = blue
tags[2][person] = ted
tags[3][person] = fred
tags[4][person] = joe
tags[5][stone] = diamond
My brain was stuck on grouping them all into tags[0]
- not sure why.