Skip to content

Additional features

dmgcodevil edited this page Sep 10, 2013 · 8 revisions

Mingo provides following convenient features:

  • Query case.

This feature allows choose query which satisfies condition for specified query parameters.

Example:

    <query id="getByMultipleParameters" type="aggregation">
        <case id="isParamsNotEmpty" type="simple" condition="empty(statuses) and empty(created)">
            {} 
        <!-- this body will be used as final query 
        if the parameters statuses(collection) and 
        created(date) will be null or empty-->
        </case>
        {$match : { "moderationStatus": { $in: "#statuses"}, "created": { "$gt" : "#created"}}}
        <!-- this body will be used as final query
        if the parameters statuses(collection) and 
        created(date) will be not null and not empty-->
    </query>
  • Query fragment.

Defines conditions(operators) which can be injected in any query in order to reduce duplicated conditions in queries.

Example:

    <queryFragment id="getCountByTagsCommon">
        {$unwind: "$tags"},
        {$project: {moderationStatus:1, tags: 1, count: {$add: [1]}}},
        {$group: {_id: "$tags", totalCount: {$sum: "$count"}}}
    </queryFragment>
    <query id="getCountByTags" type="aggregation">
        {$match : {"moderationStatus": "#moderationStatus"}},        
        <fragment ref="getCountByTagsCommon"/>
    </query>

the final query will be:

        {$match : {"moderationStatus": "STATUS_NOT_MODERATED"}}, 
        {$unwind: "$tags"},
        {$project: {moderationStatus:1, tags: 1, count: {$add: [1]}}},
        {$group: {_id: "$tags", totalCount: {$sum: "$count"}}}
  • Automaticly escape null and empty parameters from query condition.

No longer need to make null check for each parameter before adding to query condition. Mingo can remove all redundant conditions for null parameters.

Source query:

{$match : { "moderationStatus": { $in: "[STATUS_PASSED]"}, "created": { "$gt" : ""}}}, { $limit : 5 }

Processed query without redundant conditions which will be performed by Mongo driver:

{$match : { "moderationStatus": { $in: "[STATUS_PASSED]"}}, { $limit : 5 }

e.g. to achieve the same result with Spring Mongo Data you must check all parameters before they will be added to query conditions.

For example:

    public List<Review> getByMultipleParameters(Map<String, Object> parameters) {
        Criteria criteria = new Criteria();
        if (isParameterPresent(parameters, "statuses")) {
            criteria.and("moderationStatus").in((Set<ModerationStatus>) parameters.get("statuses"));
        }
        if (isParameterPresent(parameters, "created")) {
            criteria.and("created").gte(parameters.get("created"));
        }
        Query query = Query.query(criteria);
        return getMongoTemplate().find(query, Review.class);
    }
Clone this wiki locally