Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

272
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda