public async Task <IResultList <IAssetEntity> > QueryAsync(Guid appId, Guid?parentId, ClrQuery query) { using (Profiler.TraceMethod <MongoAssetRepository>("QueryAsyncByQuery")) { try { query = query.AdjustToModel(); var filter = query.BuildFilter(appId, parentId); var assetCount = Collection.Find(filter).CountDocumentsAsync(); var assetItems = Collection.Find(filter) .AssetTake(query) .AssetSkip(query) .AssetSort(query) .ToListAsync(); await Task.WhenAll(assetItems, assetCount); return(ResultList.Create <IAssetEntity>(assetCount.Result, assetItems.Result)); } catch (MongoQueryException ex) { if (ex.Message.Contains("17406")) { throw new DomainException("Result set is too large to be retrieved. Use $top parameter to reduce the number of items."); } else { throw; } } } }
public static FilterDefinition <MongoAssetEntity> BuildFilter(this ClrQuery query, Guid appId) { var filters = new List <FilterDefinition <MongoAssetEntity> > { Filter.Eq(x => x.IndexedAppId, appId), Filter.Eq(x => x.IsDeleted, false) }; var filter = query.BuildFilter <MongoAssetEntity>(false); if (filter.Filter != null) { if (filter.Last) { filters.Add(filter.Filter); } else { filters.Insert(0, filter.Filter); } } if (filters.Count > 1) { return(Filter.And(filters)); } else if (filters.Count == 1) { return(filters[0]); } else { return(new BsonDocument()); } }
public async Task <IResultList <IAssetEntity> > QueryAsync(DomainId appId, DomainId?parentId, ClrQuery query) { using (Profiler.TraceMethod <MongoAssetRepository>("QueryAsyncByQuery")) { try { query = query.AdjustToModel(); var filter = query.BuildFilter(appId, parentId); var assetCount = Collection.Find(filter).CountDocumentsAsync(); var assetItems = Collection.Find(filter) .QueryLimit(query) .QuerySkip(query) .QuerySort(query) .ToListAsync(); var(items, total) = await AsyncHelper.WhenAll(assetItems, assetCount); return(ResultList.Create <IAssetEntity>(total, items)); } catch (MongoQueryException ex) when(ex.Message.Contains("17406")) { throw new DomainException(T.Get("common.resultTooLarge")); } } }
private void AssertQuery(ClrQuery query, string expected, object?arg = null) { var rendered = query.BuildFilter <TestEntity>().Filter ! .Render(serializer, registry).ToString(); var expectation = Cleanup(expected, arg); Assert.Equal(expectation, rendered); }
private static void AssertQuery(ClrQuery query, string expected, object?arg = null) { var filter = query.BuildFilter <TestEntity>().Filter !; var rendered = filter.Render( BsonSerializer.SerializerRegistry.GetSerializer <TestEntity>(), BsonSerializer.SerializerRegistry) .ToString(); Assert.Equal(Cleanup(expected, arg), rendered); }
public static FilterDefinition <MongoAssetEntity> BuildFilter(this ClrQuery query, Guid appId, Guid?parentId) { var filters = new List <FilterDefinition <MongoAssetEntity> > { Filter.Eq(x => x.IndexedAppId, appId), Filter.Eq(x => x.IsDeleted, false) }; if (parentId.HasValue) { if (parentId == Guid.Empty) { filters.Add( Filter.Or( Filter.Exists(x => x.ParentId, false), Filter.Eq(x => x.ParentId, Guid.Empty))); } else { filters.Add(Filter.Eq(x => x.ParentId, parentId.Value)); } } var(filter, last) = query.BuildFilter <MongoAssetEntity>(false); if (filter != null) { if (last) { filters.Add(filter); } else { filters.Insert(0, filter); } } if (filters.Count > 1) { return(Filter.And(filters)); } else if (filters.Count == 1) { return(filters[0]); } else { return(new BsonDocument()); } }
public static FilterDefinition <MongoAssetEntity> BuildFilter(this ClrQuery query, DomainId appId, DomainId?parentId) { var filters = new List <FilterDefinition <MongoAssetEntity> > { Filter.Exists(x => x.LastModified), Filter.Exists(x => x.Id), Filter.Eq(x => x.IndexedAppId, appId) }; if (!query.HasFilterField("IsDeleted")) { filters.Add(Filter.Eq(x => x.IsDeleted, false)); } if (parentId != null) { if (parentId == DomainId.Empty) { filters.Add( Filter.Or( Filter.Exists(x => x.ParentId, false), Filter.Eq(x => x.ParentId, DomainId.Empty))); } else { filters.Add(Filter.Eq(x => x.ParentId, parentId.Value)); } } var(filter, last) = query.BuildFilter <MongoAssetEntity>(false); if (filter != null) { if (last) { filters.Add(filter); } else { filters.Insert(0, filter); } } return(Filter.And(filters)); }