I am trying to gain a fundamental understanding of how MongoDB works, and was unable to find this question addressed. I presume this is not possible, but maybe I am wrong?
Given the following JSON structure:
{
"bob": { "height": 68, "gender": "m" },
"sally": { "height": 48, "gender": "f" }
}
When this structure is deserialized into JavaScript, I am easily able to iterate the properties of this object to get a list of people. Note, that this list of people can/will be dynamic. Thus, sometimes "bob" won't exist, and sometimes "mike" might exist, etc.
However, with MongoDB queries, this does not seem to be possible. MongoDB appears to require data like this be in an array. Then I could query down to the "people" array, and then iterate over that.
I would like to confirm that, indeed, the MongoDB engine cannot iterate properties like JS can. If this is true, I guess this makes sense. Since the JSON structure represents both the data and the schema all in one, having dynamic properties like this might make the document difficult, if not impossible, to index. So maybe this is the reason? I am hoping someone can confirm.
I was hoping I could use MongoDB to store the same JSON structure returned my UI, but I am thinking that the structure will need to be modified to something like the following for data storage.
{ "people": [{
"name": "bob", "height": 68, "gender": "m" }, {
"name": "sally", "height": 48, "gender": "f" }]
}