public static IEnumerable <int> ArrayIntSplice( IMongoDbSet <TEntity> entity, string dbArrayFieldName, FilterDefinition <TEntity> filter, int count = 10, int skip = 0 ) { IFindFluent <TEntity, TEntity> query = entity.Find(filter); ProjectionDefinition <TEntity> projection = Builders <TEntity> .Projection.Slice(dbArrayFieldName, skip, count); return(query.FirstOrDefault() == null ? null : query .Project(projection) .FirstOrDefault() .GetValue(dbArrayFieldName) .AsBsonArray .Select(e => e.ToInt32())); }
public static bool CheckElementExists <FieldDataType>( IMongoDbSet <TEntity> entity, FieldDefinition <TEntity> dbArrayFieldName, FieldDataType element, FieldDefinition <TEntity> filterIdFieldName, FieldDataType filterIdFieldValue ) where FieldDataType : struct { FilterDefinition <TEntity> arrayFilter = Builders <TEntity> .Filter.ElemMatch <TEntity>(dbArrayFieldName, Builders <TEntity> .Filter.Eq(dbArrayFieldName.ToString(), element)); FilterDefinition <TEntity> filter = filterIdFieldName == null ? arrayFilter : Builders <TEntity> .Filter .Eq(filterIdFieldName.ToString(), filterIdFieldValue) & arrayFilter; long count = entity.Find(filter) .Project(Builders <TEntity> .Projection.Include("_id")) .Count(); return(count == 0); }