Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

308
Vistas
Mongodb AsQueryable() Performance

I have code like this where I want to query to MongoDB using Linq.

I get an AsQueryable from MongoDB collection.

public IEnumerable<IVideo> GetVideos()
{
    var collection = database.GetCollection<IVideo>("Videos");
    return collection.AsQueryable();
}

I call it like so,

var finalList = Filter2(Filter1(GetVideos())).Skip(2).Take(30);
foreach(var v in finalList)
{
    .... 
}

Functions with the queries.

public IEnumerable<IVideo> Filter1(IEnumerable<IVideo> list)
{
    return list.Where(q=>q.Categorized)
}


public IEnumerable<IVideo> Filter2(IEnumerable<IVideo> list)
{
    var query = from d in list
        where d.File == "string1" || d.File == "string2" 
                select d;
    return query;
}

My code works fine. I have my code hosted in an IIS and have around 50,000 records and the queries are a bit complex than the example. My worker process spikes to 17% and takes a few seconds to execute when the foreach is called. This is a ridiculous high for such a low date amount.

I have a couple of questions.

  1. Is the query being executed by .net or MongoDB? If it is executed by MongoDB why is my worker process taking such a hit?
  2. What are the steps I can take to improve the execution time to render the query and reduce the server load.

Thanks

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You're downloading all entries client-side by accident

public IEnumerable<IVideo> Filter1(IEnumerable<IVideo> list)
{
    var list = list.Where(q=>q.Categorized)
}

IEnumerable causes the queryable to execute and return results. Change the filter methods to accept and return IQueryable.

EDIT:

The code you posted:

public IEnumerable<IVideo> Filter1(IEnumerable<IVideo> list)
{
    var list = list.Where(q=>q.Categorized)
}

Does not compile.

Your code should look like this:

public IQueryable<IVideo> Filter1(IQueryable<IVideo> qVideos)
{
    return qVideos.Where(q => q.Categorized);
}

public IQueryable<IVideo> Filter2(IQueryable<IVideo> qVideos)
{
    return qVideos
        .Where(e => e.File == "string1" || e.File == "string2");
}

public DoSomething()
{

    // This is the query, in debug mode you can inspect the actual query generated under a property called 'DebugView'
    var qVideos = Filter2(Filter1(GetVideos()))
        .Skip(1)
        .Take(30);

    // This runs the actual query and loads the results client side.
    var videos = qVideos.ToList();

    // now iterated

    foreach (var video in videos)
    {
        
    }

}
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda