The fastest way to escape MongoDb query String?

I’m new to Mongo. Been playing with MQL, Compass and some dummy collection.

Now, I have my simple Spring Boot app which I successfully connected to database.

I decided not to use Spring Data support for Mongo queries, rather, I want to write my own “native-like” queries and would like to use @Query for placing query string itself. Query bellow works as expected in Compass, but I’m having problems escaping double quotes and other characters that needs to be escaped. Here is query taken directly from Compass command line:

{“$match”: {“_id”: ObjectId(“614513461af3bf569fdc420e”)}},
“$project”: {
“_id”: 0,
“qty”: {$last: “$instock.qty”}

The output: { qty: 35 }

What I want to achieve is something like this:

public interface OfferRepository extends MongoRepository<Offer, String> {

List<Offer> findByItemId(String itemId); //works

//other queries here

@Query({"$match": {"_id": ObjectId(?1)}},
        "$project": {
        "_id": 0,
        "qty": {$last: "$instock.qty"}
        ])") //obviously, everything has red squiggly lines...  
Object testQuery(String id);


So my question is, is there some kind of a plugin for InteliiJ, VS Code, Sublime etc. that will do this “escaping” operation instead of me trying to figure out where each ', ", \ and \ need to be placed?

Note: this reminds me when we wrote Servlets and were returning entire html bodies as part of flushed response, escaped like above. It was a nightmare, luckily that was in school. This is for a client :slight_smile:
Link to SO: