You can use $addToSet to add to a set making sure there is no duplicate array element, but that will not work for your "updating" case.
In order to do what you want, you will need to change your data structure to something like:
{
"_id" : ObjectId("4f9519d6684c8b1c9e72e367"),
"component_id" : 1,
"_locales" : {
"nl_NL" : {
"url" : "dutch"
}
}
}
Now you can do an update on the nl_NL locale with just:
db.components.update( { component_id: 1 }, { $set: { '_locales.nl_NL.url' : 'new url' } }, true );
And a new locale will work as well, such as with:
db.components.update( { component_id: 1 }, { $set: { '_locales.en_US.url' : 'American' } }, true );
You might want to consider to having the locale as part of the nested object as well perhaps, like in:
{
"_id" : ObjectId("4f9519d6684c8b1c9e72e367"),
"component_id" : 1,
"_locales" : {
"nl_NL" : {
"url" : "dutch"
"locale" : "nl_NL"
}
}
}
This makes it easier to retrieve data in some cases.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…