Search code examples
mongodbspring-bootloggingspring-data-mongodb

How to see mongo array filter in Spring boot logs?


I have an update query with an array filter

Query query = new Query(Criteria.where("_id").is(projectId));
Update update = new Update().set("requirements.$[element].status", status)
        .filterArray(Criteria.where("element._id").is(requirementId));
mongoTemplate.updateFirst(query, update, "Projects");

I turned on logging:

logging.level.org.springframework.data.mongodb.core.MongoTemplate=DEBUG

What I see in the logs:

o.s.data.mongodb.core.MongoTemplate: Calling update using query: { "_id": "633f3def5272e102ee753e98"}} and update: { "$set" : { "requirements.$[element].status" : "PENDING"}} in collection: Projects

I would like to see the array filter, but It is absent. Is there a way to see it in the logs?


Solution

  • You can set this to debug level.

    org.mongodb.driver.protocol.command: debug
    

    You will see somethinng like this,

    org.mongodb.driver.protocol.command : Sending command '{"update": "Projects", "ordered": true, "$db": "test", 
    "lsid": {"id": {"$binary": {"base64": "RhF9RRj4Td2A3ULff0ARrg==", "subType": "04"}}}, 
    "updates": [{"q": {"_id": "a"}, "u": {"$set": {"requirements.$[element].status": "s"}}, "arrayFilters": [{"element._id": "a"}]}]}'
    with request id 7 to database test on connection [connectionId{localValue:3, serverValue:27}] to server localhost:27017
    

    But, this also adds lot of unnecessary logs.

    Another option is to add the logs yourselves in your code.

    update.getArrayFilters().forEach(a -> log.info("\"arrayFilters\": "+ a.asDocument().toJson()));
    

    This will produce something like this.

    "arrayFilters": {"element._id": "a"}