0

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.

1 Answer 1

1

I think what you want to do is the same as being discussed here. This is currently not possible in Mongo, see this JIRA.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.