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 ? moderationStatus.name() :
ModerationStatus.STATUS_NOT_MODERATED.name()).get();
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("mingotest.review.getCountByTags", 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"}}}
</query>
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: mingoframework@gmail.com.
Thank you for your attention.