Skip to content
dmgcodevil edited this page Aug 28, 2013 · 4 revisions

This short example illustrates how you can create and perform mongo query. You must complete the following main steps to create and perform simple query with Mingo:

  1. Add Mingo dependency in your project.
    <repositories>
        <repository>
            <id>dropbox</id>
            <url>https://dl.dropboxusercontent.com/u/73271574/mvn-repostory</url>
        </repository>
    </repositories>
    <dependencies>
        <dependency>
            <groupId>com.git.mingo</groupId>
            <artifactId>mingo-core</artifactId>
            <version>1.0.0</version>
        </dependency>
    </dependencies>
  1. Define Mingo context configuration in XML file.
  <context>
    <querySetConfig dbName="mingotest">
        <querySet path="/mingo/reviewQuerySet.xml"/>
    </querySetConfig>
    <mongo port="27017" host="localhost"/>
  </context>
  1. Define query set.
<querySet>
    <config collectionName="review"/>

    <query id="getByMultipleParameters" type="aggregation">
        {$match : { "moderationStatus": { $in: "#statuses"}, "created": { "$gt" : "#created"}}}, { $limit : 5 }
    </query>
</querySet>
  1. Add repository with necessary methods. For example:
public interface IReviewRepository {
    List<Review> getByMultipleParameters(Map<String, Object> parameters);
}

public class ReviewRepository implements IReviewRepository {

    private static final String DB_COLLECTION_NAME = "mingotest.review.";

    private MingoTemplate mingoTemplate;

    public ReviewRepository(MingoTemplate mingoTemplate) {
        this.mingoTemplate = mingoTemplate;
    }

    @Override
    public List<Review> getByMultipleParameters(Map<String, Object> parameters) {
        return mingoTemplate.queryForList(DB_COLLECTION_NAME + "getByMultipleParameters", Review.class,
            parameters);
    }
}
  1. Load query context, create MingoTemplate and ReviewRepository.
 Context context = ContextLoader.getInstance().load("/mingo/mingo-context.xml");
 MingoTemplate mingoTemplate = new MingoTemplate(context);
 IReviewRepository reviewRepository = new ReviewRepository(mingoTemplate);
  1. Perform query.
 Map<String, Object> parameters = Maps.newHashMap();
 parameters.put("statuses", Sets.newHashSet(ModerationStatus.STATUS_PASSED));
 parameters.put("created", null);
 List<Review> reviews = reviewRepository.getByMultipleParameters(parameters);

It's all. You can compile you example and test it.

The line java parameters.put("created", null) looks at least strange, but was added in example just to demonstrate one of the most important Mingo feature. 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