public async Task <IResultList <Subscription> > QueryAsync(string appId, SubscriptionQuery query, CancellationToken ct) { var filters = new List <FilterDefinition <MongoDbSubscription> > { Filter.Eq(x => x.AppId, appId) }; if (!string.IsNullOrWhiteSpace(query.UserId)) { filters.Add(Filter.Eq(x => x.UserId, query.UserId)); } if (!string.IsNullOrWhiteSpace(query.Query)) { var regex = new BsonRegularExpression(query.Query, "i"); filters.Add(Filter.Regex(x => x.TopicPrefix, regex)); } var filter = Filter.And(filters); var resultItems = await Collection.Find(filter).ToListAsync(query, ct); var resultTotal = (long)resultItems.Count; if (query.ShouldQueryTotal(resultItems)) { resultTotal = await Collection.Find(filter).CountDocumentsAsync(ct); } return(ResultList.Create(resultTotal, resultItems.Select(x => x.ToSubscription()))); }
public async Task <IResultList <Subscription> > QueryAsync(string appId, SubscriptionQuery query, CancellationToken ct = default) { using (var activity = Telemetry.Activities.StartActivity("MongoDbSubscriptionRepository/QueryAsync")) { var filter = BuildFilter(appId, query); var resultItems = await Collection.Find(filter).ToListAsync(query, ct); var resultTotal = (long)resultItems.Count; if (query.ShouldQueryTotal(resultItems)) { resultTotal = await Collection.Find(filter).CountDocumentsAsync(ct); } activity?.SetTag("numResults", resultItems.Count); activity?.SetTag("numTotal", resultTotal); return(ResultList.Create(resultTotal, resultItems.Select(x => x.ToSubscription()))); } }