Iam using MongoDB C#/.NET Driver version 2.11.5. Here is my collection structure.
CollectionName: Deployment
{
"_id" : ObjectId("60263b3279357e12775bfb21"),
"version" : "P3-V1.0",
"productId" : "6020f75d103d4895f12c88ec",
"hierarchy" : "6020f8ad103d4895f12c88ed",
"description" : "P3-T1-V1.0",
"policies" : [
{
"name" : "Policy-1",
"description" : "Policy-1",
"rules" : [
{
"subject" : "abc.def@xyz.com",
"status" : "InProgress",
"comments" : "",
"updatedBy" : "",
"approvalId" : "8f7c8767-6613-4aa2-9b87-7778a42512bd",
"requestDate" : ISODate("2021-02-12T08:29:32.000Z")
},
{
"subject" : "ghi.jkl@xyz.com",
"status" : "InProgress",
"comments" : "",
"updatedBy" : "",
"approvalId" : "9e7c8767-6613-4aa2-9b87-7778a42512ee",
"requestDate" : ISODate("2021-02-11T08:29:32.000Z")
}
]
},
{
"name" : "Policy-2",
"description" : "Policy-2",
"rules" : [
{
"subject" : "mno.pqr@xyz.com",
"status" : "InProgress",
"comments" : "",
"updatedBy" : "",
"approvalId" : "1a6c8767-6613-4aa2-9b87-7778a42512bd",
"requestDate" : ISODate("2021-02-12T08:29:32.000Z")
},
{
"subject" : "stu.vwx@xyz.com",
"status" : "InProgress",
"comments" : "",
"updatedBy" : "",
"approvalId" : "2z0c8767-6613-4aa2-9b87-7778a42512ee",
"requestDate" : ISODate("2021-02-11T08:29:32.000Z")
}
]
}
]
}
C# Class Names:
Deployment (has multiple IList<Policy> Policies
)
Policy (has multiple IList<Rule> Rules
)
Rule
What is the Update query to update status, comments and updatedBy fields in a Rule, given Policy.Name (for ex: Policy-1) and Rule.subject (for ex: abc.def@xyz.com)?
What is the Insert query to insert a new Rule object inside a Policy, given Policy.Name (for ex: Policy-1)?
Thanks for your help.
With reference to the question -
Querying, filtering and updating multiple level nested arrays in MongoDB using C#
I have implemented the following and it worked as expected.
var updateFilter = Builders<Deployment>.Filter.Eq(f => f.Id, deployment.Id);
var updateBuilder = Builders<Deployment>.Update
.Set("policies.$[i].rules.$[j].comments", rule.Comments)
.Set("policies.$[i].rules.$[j].completionDate", rule.CompletionDate)
.Set("policies.$[i].rules.$[j].status", Enum.GetName(rule.Status))
.Set("policies.$[i].rules.$[j].updatedBy", rule.UpdatedBy);
var arrayFilters = new List<ArrayFilterDefinition>()
{
new BsonDocumentArrayFilterDefinition<Policy>(new BsonDocument("i.name", policy.Name)),
new BsonDocumentArrayFilterDefinition<Rule>(new BsonDocument("j.subject", rule.Subject))
};
var updateOptions = new UpdateOptions()
{
ArrayFilters = arrayFilters
};
await _deploymentRepository.UpdateOneAsync(updateFilter, updateBuilder, updateOptions);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.