I have an Account Object, and within that, I have many profiles. Each profile also has a group so the hierarchy is
Account->Profiles → Groups
the Class Group has GroupType and a list of profileIds assigned to it. Group has GroupType and ProfileIds which is a list of strings
I want to remove some of the strings in ProfileIDs based on the list I am interested in.
so for example, oldProfileIds = new List{“1234567”, “2345678”}
this is what I wrote, but It gives me a cast operation error: System.InvalidCastException: Specified cast is not valid.
var builder = Builders<Account>.Filter;
var filter = builder.Eq(u => u.AccountId, accountId)
& builder.ElemMatch(e => e.Profiles, p => p != null && p.ProfileId == profiled)
& builder.ElemMatch(g => g.Profiles.FirstMatchingElement().Groups, ug => ug.GroupType == groupType);
var update = Builders<Account>.Update.PullFilter(u => u.Profiles.FirstMatchingElement().Groups.FirstMatchingElement().ProfileIds, Builders<string>.Filter.Where(u=>oldProfileIds.Contains(u)));
so above doesn't work.
I also tried to use array filters as below
var update = Builders<Account>.Update.PullFilter("groups.$[i].profileIds.$[j]", Builders<string>.Filter.Where(u=>oldProfileIds.Contains(u)));
var arrayFilters = new List<ArrayFilterDefinition>
{
new BsonDocumentArrayFilterDefinition<UserGroup>(new BsonDocument("i.groupType", groupType)),
new BsonDocumentArrayFilterDefinition<UserGroup>(new BsonDocument("j.profileIds", new BsonDocument("$in", BsonArray.Create(oldProfileIds))))
};
var updateOptions = new UpdateOptions { ArrayFilters = arrayFilters };
var result = await AccountsCollection.UpdateOneAsync(filter, update,updateOptions);
but same issue -
does anyone know how to solve this using c# and linq?