Open Closed

Strange .OrderBy(x => x.Id) in some queries #5879


User avatar
0
dev3.advantiss created
  • ABP Framework version: v5.3.3
  • UI Type: Angular
  • Database System: MongoDB

Hello

I have found some code with query(It did not work in CosmosDB):


namespace Volo.Abp.PermissionManagement.MongoDB;

public class MongoPermissionGrantRepository :   MongoDbRepository<IPermissionManagementMongoDbContext, PermissionGrant, Guid>,
    IPermissionGrantRepository
{
    public MongoPermissionGrantRepository(IMongoDbContextProvider<IPermissionManagementMongoDbContext> dbContextProvider)
        : base(dbContextProvider)
    {

  }

    public virtual async Task<PermissionGrant> FindAsync(
        string name,
        string providerName,
        string providerKey,
        CancellationToken cancellationToken = default)
    {
        cancellationToken = GetCancellationToken(cancellationToken);
        return await (await GetMongoQueryableAsync(cancellationToken))
            **.OrderBy(x => x.Id)**
            .FirstOrDefaultAsync(s =>
                s.Name == name &&
                s.ProviderName == providerName &&
                s.ProviderKey == providerKey,
                cancellationToken
            );
    }

MongoDB get next query:

"aggregate":"AbpPermissionGrants",
      "pipeline":[
         {
            "$match":{
               "TenantId":null
            }
         },
         {
            "$sort":{
               "_id":1
            }
         },
         {
            "$match":{
               "Name":"Dibl.Themes.View",
               "ProviderName":"R",
               "ProviderKey":"Facilitator"
            }
         },

Why did you use OrderBy(x => x.Id) in this query? And I think "$sort" hurts query performance...

Best regards, Advantiss Inc


1 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    The query uses the 'First'/'FirstOrDefault' operator without 'OrderBy' and filter operators. This may lead to unpredictable results.

    This is the reason that we use orderby.

Made with ❤️ on ABP v9.1.0-preview. Updated on December 12, 2024, 07:15