Click here to Skip to main content
13,346,713 members (26,805 online)
Click here to Skip to main content
Add your own
alternative version


3 bookmarked
Posted 10 Sep 2013

Framework for Performing Aggregation and Plain MongoDB Queries in myBatis Style

, 12 Sep 2013
Rate this:
Please Sign up or sign in to vote.
Framework for performing aggregation and plain MongoDB queries in myBatis style

Mingo Framework Overview

Introduction to Mingo

Mingo is a free open source framework for performing aggregation and plain MongoDB queries which makes creation queries process simple and convenient for developers.

Why Mingo?

Usually to implement and use new MongoDB query developer has to:

  • Write and validate the query in MongoDB console.
  • Implement the query in Java using plain Java driver or some ORM framework (Spring Data, Morphia, etc.).
  • Do testing.

Common Aggregation Query when Implemented using Spring Mongo Data looks like:

public Map<String, Integer> getCountByTags(ModerationStatus moderationStatus) {
    DBObject matchOperator = start().push(MATCH.getMongoName())
    .add("moderationStatus", moderationStatus != null ? :;
    DBObject unwindOperator = start().add(UNWIND.getMongoName(), "$tags").get();
    DBObject projectOperator = start().push(PROJECT.getMongoName()).
    add("moderationStatus", 1).add("tags", 1)
        .push("count").add("$add", new Integer[]{1}).pop().pop().get();
    DBObject groupOperator = start().push(GROUP.getMongoName()).
    add("_id", "$tags").push("totalCount")
        .add(SUM.getMongoName(), "$count").pop().pop().get();
    List<DBObject> operators = Lists.newArrayList
    (matchOperator, unwindOperator, projectOperator, groupOperator);
    Map<String, Integer> result = reviewConverter.convertCountByTags(
        getAsBasicDBList(aggregate("review", operators)));
    return result;

This code is very hard to read and maintain (our team has nothing against the Spring Mongo Data, we are just stating the fact that large queries which were implemented in fluent style are very hard to read and support). Every time when the developer wants to make changes in query, he should write and validate query in MongoDB console and later implement these changes in code. Very often (especially in the case of large queries), this action requires too much time. Of course, you can change code directly, but it isn't so easy, fast and clear as in the console. How many times you must change and compile code, run tests before you find the acceptable variant of query? Or can be faster do it in mongo console and save as is in XML file to keep code clean? Let's see how the same query looks when it is implemented with Mingo:

public Map<String, Integer> getCountByTags(ModerationStatus moderationStatus) {
 Map<String, Object> parameters = Maps.newHashMap();
 parameters.put("moderationStatus", moderationStatus);        
 return mingoTemplate.queryForObject("", Map.class, parameters);

Feel the difference. 12 lines of code, or 3? We think the answer is obvious.

We removed query creation logic from code to XML file:

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

Mingo resolves the following issues:

  • Long development cycle
  • Query creation logic clutters code
  • Inability to choose different queries in runtime based on query parameters
  • Every time make null check for each parameter before adding to query condition / validates parameters

The project code exists and is available to clone on github.

Also, you can read more in the project Wiki.

Now Mingo doesn't support insert and update operations because originally it was planned as an addition to some ORM framework (Spring Data, Morphia, etc.). which is already used in the project. But we are going to change this situation in the future: add insert and update operations in the next release (all list of features which will be added in the next release will be described in project Wiki).

I am actively looking for developers that I can team up with to continue updates, etc. on the Mingo framework. I will be glad to hear your ideas and suggestions, it is very important to me. Mingo is young and rapidly developing framework and I think together we can make it better, safer and faster. Please fell free to write your questions in the comments or send directly on email:

Thank you for your attention.


This article, along with any associated source code and files, is licensed under The Apache License, Version 2.0


About the Author

Software Developer EPAM System
Belarus Belarus
This member doesn't quite have enough reputation to be able to display their biography and homepage.

You may also be interested in...


Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.180111.1 | Last Updated 12 Sep 2013
Article Copyright 2013 by dmgcodevil
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid