Last active
March 24, 2020 12:53
-
-
Save ismailmayat/ea5ac01df6219708c49e7f210cfff613 to your computer and use it in GitHub Desktop.
Mongo delete from array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"companyName": "LinkedIn", | |
"managedBy": { | |
"name": "Novicel agency", | |
"email": "[email protected]", | |
"umbracoMemberId": 1052, | |
"umbracoMemberGuidId": "d5e2ec1b-44d8-4a71-806f-fc5a3f252e98", | |
"agency": "Agency", | |
"agencyId": 1051 | |
}, | |
"contractors": [ | |
"1494403d-e34b-4408-87e3-637d2b9be449", | |
"27798ab9-68f3-4673-bc6e-21eaf1cea908", | |
"0d260ee7-c5b7-4ae5-a081-18be1ebac75d" | |
], | |
"id": "8064485a-8b23-4ff2-8b70-e29633a0a2c7", | |
"updatedOn": "2020-03-13T11:47:54.36Z", | |
"createdOn": "2020-03-10T10:24:01.292Z" | |
} | |
public void RemoveContractor(Guid contractorId) | |
{ | |
FilterDefinition<Company> companiesAssignedToContractor = new ExpressionFilterDefinition<Company>(c=>c.Contractors.Contains(contractorId)); | |
var removeContractor = Builders<Company>.Update.PullFilter(f=>f.Contractors,p => p.Equals(contractorId)).Set(d=>d.UpdatedOn,DateTime.Now); | |
_companyRepo.UpdateMany(companiesAssignedToContractor, removeContractor); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Looks like you want a filter and update query like the following