Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mongodb cannot filter data #17203

Closed
1 task done
XiaoChenDan opened this issue Jul 27, 2023 · 5 comments · Fixed by #17207
Closed
1 task done

Mongodb cannot filter data #17203

XiaoChenDan opened this issue Jul 27, 2023 · 5 comments · Fixed by #17207
Assignees
Labels
Milestone

Comments

@XiaoChenDan
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Description

I create MongoDbRepositoryFilterer<TEntity, TKey> based on https://docs.abp.io/en/abp/latest/Data-Filtering , but when I do a data query GetListAsync() , process did not enter . it return all items.
Whether the data filtering for get operation is missing?
#11595

ProjectRepositoryFilterer:

[ExposeServices(typeof(IMongoDbRepositoryFilterer<Project, Guid>))]
public class ProjectRepositoryFilterer : MongoDbRepositoryFilterer<Project, Guid>, ITransientDependency
{
    private readonly ISettingProvider _settingProvider;
    private readonly ICurrentUser _currentUser;
    public ProjectRepositoryFilterer(
        IDataFilter dataFilter,
        ICurrentTenant currentTenant,
        ICurrentUser currentUser,
        ISettingProvider settingProvider) : base(dataFilter, currentTenant)
    {
        _currentUser = currentUser;
        _settingProvider = settingProvider;
    }

    public override async Task AddGlobalFiltersAsync(List<FilterDefinition<Project>> filters)
    {
        await base.AddGlobalFiltersAsync(filters);

        if (!(await _settingProvider.GetOrNullAsync(ProjectManagementSettings.ManagerRoles))
            .Split(',').Any(_currentUser.IsInRole))
        {
            filters.Add(Builders<Project>.Filter.Or(
                Builders<Project>.Filter.Eq(x => x.CreatorId, _currentUser.Id),
                Builders<Project>.Filter.Eq(x => x.ProjectManager, _currentUser.Id),
                Builders<Project>.Filter.Eq(x => x.ProjectBimManager, _currentUser.Id),
                Builders<Project>.Filter.Where(x => x.Members.Contains(_currentUser.Id!.Value))));
        }
    }
}

ProjectRepository:

public class ProjectRepository : MongoDbRepository<ProjectManagementMongoDbContext, Project, Guid>, IProjectRepository
    {
        private readonly ProjectManager _projectManager;

        public ProjectRepository(
            IMongoDbContextProvider<ProjectManagementMongoDbContext> dbContextProvider,
            ProjectManager projectManager) : base(dbContextProvider)
        {
            _projectManager = projectManager;
        }
    }

ProjectManagementMongoDbContext

[ConnectionStringName(ProjectManagementDbProperties.ConnectionStringName)]
public class ProjectManagementMongoDbContext : AbpMongoDbContext, IProjectManagementMongoDbContext
{
    /* Add mongo collections here. Example:
     * public IMongoCollection<Question> Questions => Collection<Question>();
     */

    public IMongoCollection<Project> Projects => Collection<Project>();

    protected override void CreateModel(IMongoModelBuilder modelBuilder)
    {
        base.CreateModel(modelBuilder);

        modelBuilder.ConfigureProjectManagement();
    }
}

Reproduction Steps

No response

Expected behavior

No response

Actual behavior

No response

Regression?

No response

Known Workarounds

No response

Version

7.3.0.0

User Interface

Common (Default)

Database Provider

MongoDB

Tiered or separate authentication server

None (Default)

Operation System

Windows (Default)

Other information

No response

@masoodkhoshgard
Copy link
Contributor

Hi @XiaoChenDan

You must register this filter in the services like the following code example:

public class ProjectManagementMongoDbModule : AbpModule
{
    public override void ConfigureServices(ServiceConfigurationContext context)
    {
        //.......
        context.Services
            .Replace(ServiceDescriptor.Transient(typeof(IMongoDbRepositoryFilterer<,>),typeof(ProjectRepositoryFilterer<,>)));
    }
}

@XiaoChenDan
Copy link
Author

Hi @masoodkhoshgard , thanks you for your reply
I don't think the problem is that the service has not been successfully injected. In fact, I also tried it, and it failed again.
I try to understand IMongoDbRepositoryFilterer implementation to better solve the problem, and I only find IMongoDbRepositoryFilterer<TEntity, TKey> in https://github.com/abpframework/abp/blob/a2875f1d276e0a8fbe98ea3d7b4ea97964d5b230/framework/src/Volo.Abp.MongoDB/Volo/Abp/Domain/Repositories/MongoDB/MongoDbRepository.cs#L749
and in CreateEntityFilterAsync () , CreateEntitiesFilterAsync () method to use the service,and it just filters the 'ConcurrencyStamp' parameters.
I also found that CreateEntitiesFilterAsync methods seem to be differences between the target and actual , 'withConcurrencyStamp' or 'applyFilters' Parameters.
Where was I wrong?

@maliming
Copy link
Member

hi @XiaoChenDan

We will introduce a FilterQueryable method to IMongoDbRepositoryFilterer.

@XiaoChenDan
Copy link
Author

hi @maliming
That's great! It solved my problem.
By the way, are there any plans to switch to asynchronous methods

@maliming
Copy link
Member

By the way, are there any plans to switch to asynchronous methods

Sync methods will call it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants