I have a collection in monogoDB with the following document structure:
{_id:"foo",
content: [
{_id: "bar1",
field1: "value1",
field2: "value2",
field3: "value3",
field4: "value4"},
{_id: "bar2",
field1: "value5",
field2: "value6",
field3: "value7",
field4: "value8"}]
}
I want to perform a partial update in a single embedded content document without having to specify which fields I am updating. From what I have read, my only option for doing this is:
Update({"_id": "foo", "content._id" : "bar1"},
{"$set": {"content.$.field1" : "value9", "content.$.field2" : "value10"})
Assuming this is infact my best option, what is the best way to apply updates from a POST body? (I'm doing this in go). I don't think it makes sense to have the client prefix all of the field names with "content.$.", so right now I am creating a new map with the prefixed fields after receiving the POST body:
content := bson.M{}
json.NewDecoder(r.Body).Decode(&content)
newContent := bson.M{}
for key, val := range content{
newContent["content.$." + key] = val
}
//proceed with newContent
but this feels like a hack. Is there a better way to do this?