How to dynamically add additional filter to an existing mongo query through Mongo interceptor/listener

I have a scenario where depending on an incoming request’s HTTP header, I need to attach an additional filter to the query (Collection.find()), and when creating a new document I would like to set an additional field to document when saving to the DB.

We have close to 30+ Spring boot microservices that need to accommodate a change, depending on the incoming “tenant” HTTP header we need to filter the data by adding the tenant field query filter.

I am looking for a Mongo query interceptor where I can attach the additional filter to the in-flight Mongo query that is being sent to the server.

I tried utilizing

CommandListener#commandStarted(CommandStartedEvent event)

to update the query but that doesn’t seem to update the query (immutable?). Most implementations I came across are reading/logging the event but none are updating the event command.

Is this achievable with Mongo core java driver (version: 4.3.1 Java driver)?
Appreciate any pointers

 @Override
    public void commandStarted(CommandStartedEvent event) {
      
            final String tenantId = threadContext.get(CONTEXT_REQUEST_HEADER_PREFIX + HEADER_TENANT_ID);

                String commandName = event.getCommandName();
                final BsonDocument command = event.getCommand();

                if ("find".equals(commandName)) {
                    BsonDocument tenantFilter = new BsonDocument("metaInfo.tenantId", new BsonString(tenantId));
                    final BsonDocument filter = command.getDocument("filter");

                    final BsonDocument append = filter.append("metaInfo.tenantId", tenantFilter);

                    event = new CommandStartedEvent(event.getRequestId(), event.getConnectionDescription(),
                                                    event.getDatabaseName(), event.getCommandName(), append);
                }

            
        }

Hi @Raja and welcome to MongoDB community forums!!

I see that your query has been unattended for a long time and I hope you have been able to resolve the issue.

If however you have not been able to resolve the issue, I would recommend to follow the below considerations for further assistance.

Unfortunately, the MongoDB Java driver (version 4.4 or any other released version) does not have native support for a query interceptor mechanism. Hence, you would need to modify the query at the application end according to your specific use case.

You can make changes to the above code if the command is Find()

Bson originalFilter = (Bson) event.getCommand().get("filter");
Bson additionalFilter = Filters.eq("additionalField", "additionalValue");
Bson newFilter = Filters.and(originalFilter, additionalFilter);
event.getCommand().put("filter", newFilter);

Please let us know if the issue has been resolved or post the response for the community if you have found a better solution. :blush:

Also, this looks like a large deployment involving huge amount of microservice, would also recommend to consider performance issues like cascading issues etc for the application.

Best Regards
Aasawari