Only query field if value has been passed from API in MongoDB
<p>I have created an API in Mule and have a number of queryParameters specified in the RAML file that I would like to use to query my store MongoDB collection.</p>

<p>A snippet of the collection looks like this:</p>

<pre><code>{
    "stores": [{
        "storeId": 1234,
        "storeName": "Shop Around Ltd",
        "opens": "09:00:00.000Z",
        "closes": "17:00:00.000Z",
        "departments": ["clothing", "computers", "toys", "kitchen and home"],
        "address": {
            "street": "street1",
            "city": "New York",
            "state": "New York",
            "zipCode": "10002"
        }
    }]
}
</code></pre>

<p>My problem is that the query parameters used to query the collection may be different each time so how can I write a dynamic query for MongoDB so it only queries on the fields that have been passed in with values instead of writing a query for each combination of query parameters?  E.g. I may want to query based on zip code for one query so all the other fields will be blank and the next time I may want to query on the departments and whether the store will be open at 11am.</p>

<p>The query will be called from Mule.</p>

<p>Thanks</p>