Ankit K
Ankit K

Reputation: 1326

Mongo c# query- aggregate match how to pass In statement to check uid existence in list<string>

List<string> uidsToMatch= //recieving it as method parameter

Now i am generating mongo group by query using Aggregate with Match. Inside Match i need to check uid existence in uidsToMatch similar to sql in operator. If i use it like .Match(c => c.Uid == "abc") to match single value it works fine but i need to use something similar to In operator to match this c.Uid in above uidsToMatch list. Please suggest how to do it.

var anonymousTypeList = mongoCollection.Aggregate().Match(c => c.Uid == ------).Group(key => new
                        {
                            Uid = key.Uid
                        },
                        group => new { Key = group.Key, MinIndex = group.Min(p => p.Time), MaxIndex = group.Max(p => p.Time) })
                        .ToList();

Upvotes: 0

Views: 3283

Answers (1)

Neil Lunn
Neil Lunn

Reputation: 151122

You want Any() and Contains() here:

string[] listUid = new [] { "abc", "def" };

// Then in the Match

.Match(c => c.listUid.Any(i => c.Uid.Contains(i));

Works out to be the same as

{ "$match": { "Uid": { "$in": ["abc", "def"] } } }

Or even:

.Match(c => listUid.Contains(c.Uid))

For a little shorter and the same thing

Upvotes: 2

Related Questions