Storing a query in Mongo
<p>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 allows me to experiment with different parameters also change their values easily. </p>

<p>I have a Product collection that I want to query based on multiple parameters. 
A couple of these are found here:</p>

<p>within product:</p>

<pre><code>"delivery" : {
    "maximum_delivery_days" : 30,
    "average_delivery_days" : 10,
    "source" : 1,
    "filling_rate" : 85,
    "stock" : 0
}
</code></pre>

<p>but also other parameters exist.</p>

<p>An example of such query to decide whether or not to include a product could be:</p>

<pre><code>"$or" : [
        {
            "delivery.stock" : 1
        },
        {
            "$or" : [
                    {
                            "$and" : [
                                    {
                                            "delivery.maximum_delivery_days" : {
                                                    "$lt" : 60
                                            }
                                    },
                                    {
                                            "delivery.filling_rate" : {
                                                    "$gt" : 90
                                            }
                                    }
                            ]
                    },
                    {
                            "$and" : [
                                    {
                                            "delivery.maximum_delivery_days" : {
                                                    "$lt" : 40
                                            }
                                    },
                                    {
                                            "delivery.filling_rate" : {
                                                    "$gt" : 80
                                            }
                                    }
                            ]
                    },
                    {
                            "$and" : [
                                    {
                                            "delivery.delivery_days" : {
                                                    "$lt" : 25
                                            }
                                    },
                                    {
                                            "delivery.filling_rate" : {
                                                    "$gt" : 70
                                            }
                                    }
                            ]
                    }
            ]
        }
]
</code></pre>

<p>Now to make this configurable, I need to be able to handle boolean logic, parameters and values. 
So, I got the idea, since such query itself is JSON, to store it in Mongo and have my Java app retrieve it. 
Next thing is using it in the filter (e.g. find, or whatever) and work on the corresponding selection of products.
The advantage of this approach is that I can actually analyse the data and the effectiveness of the query outside of my program.</p>

<p>I would store it by name in the database. E.g. </p>

<pre><code>{ 
    "name": "query1",
    "query": { the thing printed above starting with "$or"... }
}
</code></pre>

<p>using:</p>

<pre><code>db.queries.insert({
    "name" : "query1",
    "query": { the thing printed above starting with "$or"... }
})
</code></pre>

<p>Which results in:</p>

<pre><code>2016-03-27T14:43:37.265+0200 E QUERY    Error: field names cannot start with $ [$or]
    at Error (&lt;anonymous&gt;)
    at DBCollection._validateForStorage (src/mongo/shell/collection.js:161:19)
    at DBCollection._validateForStorage (src/mongo/shell/collection.js:165:18)
    at insert (src/mongo/shell/bulk_api.js:646:20)
    at DBCollection.insert (src/mongo/shell/collection.js:243:18)
    at (shell):1:12 at src/mongo/shell/collection.js:161
</code></pre>

<p>But I CAN STORE it using Robomongo, but not always. Obviously I am doing something wrong. But I have NO IDEA what it is.
If it fails, and I create a brand new collection and try again, it succeeds. Weird stuff that goes beyond what I can comprehend.</p>

<p>But when I try updating values in the "query", changes are not going through. Never. Not even sometimes.
I can however create a new object and discard the previous one. So, the workaround is there.</p>

<pre><code>db.queries.update(
    {"name": "query1"},
    {"$set": { 
            ... update goes here ...
        }
    }
)
</code></pre>

<p>doing this results in: </p>

<pre><code>WriteResult({
        "nMatched" : 0,
        "nUpserted" : 0,
        "nModified" : 0,
        "writeError" : {
                "code" : 52,
                "errmsg" : "The dollar ($) prefixed field '$or' in 'action.$or' is not valid for storage."
        }
})
</code></pre>

<p>seems pretty close to the other message above. </p>

<p>Needles to say, I am pretty clueless about what is going on here, so I hope some of the wizzards here are able to shed some light on the matter</p>