Storing a query in Mongo

后端 未结 4 1763
南笙
南笙 2021-01-13 10:39

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

4条回答
  •  深忆病人
    2021-01-13 11:11

    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);
    

提交回复
热议问题