I don’t think there is a way to run the whole string as a query using Java or Spring Java APIs. But, you can run native queries using available APIs. For example, using Java Driver you can use the db.runCommand. Here is a post with its usage:
There is similar API method with Spring Data MongoDB, using the MongoTemplate class.
Thank you @MaBeuLux88, Blogs are really helpful. In my Scenario complex part is I don’t know the collection name & what Operation(Find or Count) query intends to(and also I should not parse the string to get the collection name and operation).
ie.
For the query db.getCollection("books").find( { author: "Leo Tolstoy" } ), you can construct a find command query as, for example, db.runCommand({ "find": "books", "filter": { "author": "Leo Tolstoy" } }).
In the above code, the variable strCmdis constructed using the collection name and the query filter strings as "books" and "{ 'author': 'Leo Tolstoy' }" respectively, from the actual shell query.