Reputation: 2577
I have an interesting scenario where I have a 2-level embedded array in a Mongo Document and I can't seem to figure out a way to update elements inside the 2nd level. I've tried ElemMatch but it doesn't seem to be working.
I have a Product document which has an Attributes array. This Attributes Array has multiple options. Ex. a Product could be a t-shirt, with attributes Size, Color. Each Attribute could have options. For e.g. Size has options XS, S, M, L. Now I want the ability to update the size option given the OptionId (and/or the Attribute Id).
Here's how my Mongo document looks like:
{
"Attributes" : [{
"_id" : ObjectId("52adf86912a4590a3cb322a5"),
"Name" : "Size",
"LabelText" : "Select your T-Shirt Size",
"Description" : "This is the Size of the T-Shirt",
"ShowOnDisplay" : true,
"Options" : [{
"_id" : ObjectId("52adf86912a4590a3cb322a6"),
"Name" : "XS",
"Value" : "XS"
}, {
"_id" : ObjectId("52adf86912a4590a3cb322a7"),
"Name" : "S",
"Value" : "S"
}],
"DateCreatedUtc" : new Date("12/15/2013 10:43:53"),
"DateModifiedUtc" : new Date("12/15/2013 10:43:53")
}],
"AvailableEndTimeUtc" : null,
"AvailableStartTimeUtc" : null,
"Name" : "CMR Dazzle T-Shirt ",
"Price" : {
"Value" : "25",
"Currency" : "USD"
},
"Sku" : "SKU7fb3a",
"_id" : ObjectId("52adf86912a4590a3cb322a4")
}
In the example above, I'd like to update the XS to say "X-Small". I have both the Attribute Id and the Option Id available.
I've tried using ElemMatch, I've tried simple Query.EQ("Attributes.Options._id") and then used update and nothing works. Similar to that, I'd also like to delete an option given an id.
I would greatly appreciate any help here.
Upvotes: 0
Views: 242