I have a classic REST and ODATA enabled Web API controller calling MongoDB based implementation of a repository pattern.
I keep on getting
Overflow sort stage buffered data usage of 33556193 bytes exceeds internal limit of 33554432 byte Exception
when i try to skip first 12010+ records and get top 10
?$skip=12020&$top=10&$orderby=Serial
After some search I tried to implement an index on Serial like
private void GetCollection() //is like DBSet of some entity
{
_collection = _dbContext.Database
.GetCollection<TEntity>(typeof(TEntity).Name);
Type typeParameterType = typeof(TEntity);
if (typeParameterType.Name == "StoreCommand")
if (_collection.IndexExists("Serial") == false)
_collection.CreateIndex(IndexKeys<StoreCommand>.Ascending(_ => _.Serial));
}
My repository implementation is like this.
public class MongoDbRepository<TEntity> :
IRepository<TEntity> where
TEntity : EntityBase
{
private MongoCollection<TEntity> _collection;
private SNDbContext _dbContext;
public MongoDbRepository(SNDbContext dbContext)
{
_dbContext = dbContext;
GetCollection();
}
private void GetCollection()
{
_collection = _dbContext.Database
.GetCollection<TEntity>(typeof(TEntity).Name);
}
public IQueryable<TEntity> GetAll()
{
return _collection.AsQueryable();
}//............And other functions after this
}
call from service layer is like this
IQueryable<StoreCommand> GetAllStoreCommands()
{
return _uow.StoreCommands.GetAll();
}
where SNDbContext has all the code related to getting me the Database using MongoClient and connection string.