I have a document "owner" that can have "n" number of camps and camps have "instructors" and instructors have "classes". Earlier I tried accomplishing this with nested arrays (see link to my post below), however I learned that the positional operator "$" does not nest that deep. MongoDB: Adding an array into an existing array
However, I learned that the workaround would be to use object collections instead of arrays. I'm assuming that I have to use "update" with "$set" to add additional "camps", however every time I do all it does is overwrite(update) the previous camp that was inserted.
Below is the hierarchical structure I am trying to accomplish:
owner = {
firstName: 'john',
lastName: 'smith',
ownerEmail: 'john.smith@gmail.com',
camps : {
{
name: 'cubs-killeen',
location: 'killeen'
},
{
name: 'cubs-temple',
location: 'temple'
},
instructors : {
{
firstName: 'joe',
lastName : 'black'
},
{
firstName: 'will',
lastName : 'smith'
}
}
}
}
I have also been trying the following:
db.owners.update({ownerEmail:'john.smith@gmail.com'}, {$set: { camps:{ {name:'cubs-killeen'} } }})
but this throws an unexpected identifier { error.
Any help with some sample mongo commands to achieve the structure above would be most appreciated.
V/R
Chris
As other mentioned, The structure you want is not valid. I recommend the following structure for your owner document:
and then
Having this, you can add camps like this:
Hope it helps.