I have a collection of documents in MongoDB with a structure like this (there are multiple container documents, each containing Sessions
, each session contains 1 Order, each Order contains multiple Items):
{
"Sessions" : [{
"ID" : "1882a092-d395-45d1-b36b-e2fa3df81b95",
"Order" : {
"Items" : [{
"_id" : "a9c94a5e-10ef-433d-9c63-f4555eb7c2a7",
"Title" : "UPDATE THIS",
}]
}
}],
"_id" : "1b640bc4-fdb4-49b1-9c60-e4c6f1bd0405"
}
I would like to update Title
of a given Item
within an Order in a Session while knowing the _id
of the Item (I also know ID of the Session if that helps):
I have tried the following:
col.Update(Query.EQ("Sessions.ID", sessionID),
Update.Set("Sessions.$.Order.Items.Title", newTitle));
col.Update(Query.EQ("Sessions.Order.Items._id", id),
Update.Set("Sessions.Order.Items.$.Title", newTitle));
Both throw an exception WriteConcern detected an error 'cannot use the part (Sessions of Sessions.Order.Items.0.Status) to traverse the element ...
. I also tried different combinations of combined queries using Query.And
which probably make so little sense that they are not worth posting here.
How to update field in a subdocument contained in an array of documents using the C# driver?
I realize this cannot be done with the positional operator alone (and the positional operator will not magically match inner array).
I am looking for out of the box solutions how to accomplish this kind of update without changing the schema.