raresm
raresm

Reputation: 423

MongoDB 2 - query array without hardcoding name

I have an Issue entity with contains a List of Guids named AssignedTo. For one ID, I need to retrieve all issues with that ID inside AssignedTo list/array

So far I've managed doing this with:

var filter = Builders<Issue>.Filter.Eq("AssignedTo", id.ToString());
return await Collection.Find(filter).ToListAsync();

but I do not want to hardcore AssignedTo in the query. p => p.AssignedTo does not work, I've tried that too.

Also, how do I tell to BsonClassMap to register that list as a list of strings and not GUIDS? I know about this:

x.GetMemberMap(p => p.AssignedBy).SetSerializer(new GuidSerializer(BsonType.String));

but how I do it on lists?

I'm using the latest MongoDB driver for C#. (2.0)

Upvotes: 0

Views: 473

Answers (1)

Craig Wilson
Craig Wilson

Reputation: 12624

You are looking for the AnyEq method. See the docs here: http://mongodb.github.io/mongo-csharp-driver/2.0/reference/driver/definitions/#array-operators

var filter = Builders<Issue>.Filter.AnyEq(x => x.AssignedTo, id.ToString());

Upvotes: 2

Related Questions