Remove a Guid(uuid) from a nested list via Mongo DB C# Driver

Viewed 155

I have a MongoDB Model which consists of an Id(Guid) and a list of Ids (which are also Guids). Now... for some reason I want to query the list of UserIds and if there is an element with a certain Guid I want to remove it from my Mongo Array.

So here's a part of my model:

public class Model {
        [BsonId]
        [BsonRepresentation(BsonType.String)]
        public Guid Id { get; set; }

        [BsonElement("userIds")]
        public List<Guid> UserIds{ get; set; }
}

and here's the piece of code from the repository where the list of Guids should get an element removed.

var filter = Builders<Models.Event>.Filter.Eq(m => m.Id, someId);
var update = Builders<Models.Event>.Update.PullFilter(m => m.UserIds,
                                                i => i == userId);

await _events.FindOneAndUpdateAsync(filter, update);

In theory, this should work... The problem is Mongo keeps giving me the following error:

"error": "{document} is not supported.",

For some reason, my PullFilter doesn't seem to be able to identify my Guid in the DB.

Here's how the Guid is stored in the DB:

userIds:Array
0:Binary('WYsLZis0RkOeiQ1RNyjzGw==', 3)

Is this a conversion issue? Should I use a string instead of a Guid here? I can't figure out exactly what's happening behind the scenes and why this wouldn't work.

I also tried altering the code to have the Guid be converted to string and check the value against my userId.ToString() but then, i get {document}.ToString() is not supported.

Has anyone encountered this before?

1 Answers

Try doing this instead of your update definition:

var update = Builders<Models.Event>.Update.Pull(m => m.UserIds, userId);

Related