This is the case: A webshop in which I want to configure which items should be listed in the sjop based on a set of parameters. I want this to be configurable, because that
Obviously my attempt to store a query in mongo the way I did was foolish as became clear from the answers from both @bigdatakid and @lix. So what I finally did was this: I altered the naming of the fields to comply to the mongo requirements.
E.g. instead of $or I used _$or etc. and instead of using a . inside the name I used a #. Both of which I am replacing in my Java code.
This way I can still easily try and test the queries outside of my program. In my Java program I just change the names and use the query. Using just 2 lines of code. It simply works now. Thanks guys for the suggestions you made.
String documentAsString = query.toJson().replaceAll("_\\$", "\\$").replaceAll("#", ".");
Object q = JSON.parse(documentAsString);