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

273
Vistas
Working with .net core and mongodb understanding the performance

This is the first time when I have .net core and MongoDB usually use Entity Framework ORM. I use .net core 3 and MongoDB driver 2.11 to work with the database.

Here is an example of a repository service:

public class Repository<T> : IRepository<T> where T : IDocument
{
    private readonly IMongoCollection<T> _collection;

    public Repository(IDatabaseSettings settings)
    {
            var client = new MongoClient(settings.ConnectionString);
            var database = client.GetDatabase(settings.DatabaseName);
            _collection = database.GetCollection<T>(GetCollectionName(typeof(T)));
    }

    Task<T> FindOneAsync(Expression<Func<T, bool>> filterExpression){}
    Task<T> FindByIdAsync(string id){}
    Task InsertOneAsync(T document) {}
    Task InsertManyAsync(ICollection<T> documents){}
    Task ReplaceOneAsync(T document) {}
    Task DeleteOneAsync(Expression<Func<T, bool>> filterExpression){}
    Task DeleteByIdAsync(string id) {}
    Task DeleteManyAsync(Expression<Func<T, bool>> filterExpression){}
    
}

As you can see this is a standard definition of repository class.

My question is when the repository works with a specific collection does all collection is loaded to local memory?

For example, if I want to find a specific document in the collection, then GetCollection in constructor fetching collection from database and running on all documents in the collection locally?

Or does it generate a query and execute it in a database similarly to Entity Framework ORM?

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

0

My question is when the repository works with a specific collection does all collection is loaded to local memory?

No.

It just takes a "handle" to the collection. It does not fetch any document.

For example, if I want to find a specific document in the collection, then GetCollection in constructor fetching collection from database and running on all documents in the collection locally?

Neither.

You would need to use FindOne or FindById. Those will issue a query to fetch documents (one for that matter)

btw you may want a Find as well (thus to get more than only one doc)

Or does it generate a query and execute it in a database similarly to Entity Framework ORM?

GetCollection does not generate a query to fetch anything


It seems a bit "authoritative" so you may just open a mongo shell and play around:

// just set up some data to collection dummy
> db.dummy.insert([{a:2},{a:1}])
// look for any document having field 'a' with value 1.
// only one doc returned, fair enough
> db.dummy.find({ a: 1 })
{ "_id" : ObjectId("60334ea3623e06e621f86a0b"), "a" : 1 }

// this is equivalent to
> db.getCollection('dummy').find({ a: 1 })
// without the find() you just get information on the collection, NO documents fetched
// imagine collection as an object, on which you can issue queries (find, findOne, remove, ...)
// but not necessarily query to touch documents: e.g ensureIndex, help, ...
// (just type <<tab>> to see available commands)
> db.getCollection('dummy')
dummy.dummy

As a side note, getCollection allows to target collection names with namings a bit peculiar like

db._dummy.insert({ a: 1 }) // fails because of '_' char
uncaught exception: TypeError: db._dummy is undefined :
db.getCollection('_dummy').insert({ a: 1 })
WriteResult({ "nInserted" : 1 })
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