About Vlad Mihalcea

Vlad Mihalcea is a software architect passionate about software integration, high scalability and concurrency challenges.

MongoDB 2.6 is out

Introduction

MongoDB is evolving rapidly. The 2.2 version introduced the aggregation framework as an alternative to the Map-Reduce query model. Generating aggregated reports is a recurrent requirement for enterprise systems and MongoDB shines in this regard. If you’re new to it you might want to check this aggregation framework introduction or the performance tuning and the data modelling guides.

Let’s reuse the data model I first introduced while demonstrating the blazing fast MongoDB insert capabilities:
 

{
        "_id" : ObjectId("5298a5a03b3f4220588fe57c"),
        "created_on" : ISODate("2012-04-22T01:09:53Z"),
        "value" : 0.1647851116706831
}

MongoDB 2.6 Aggregation enhancements

In the 2.4 version, if I run the following aggregation query:

db.randomData.aggregate( [ 
{ 
	$match: { 
		"created_on" : { 
			$gte : new Date(Date.UTC(2012, 0, 1)), 
			$lte : new Date(Date.UTC(2012, 0, 10)) 
		} 
	} 
},  
{ 
	$group: {
		_id : {
			"minute" : {
				$minute : "$created_on"
			} 
		},  
		"values": { 
			$addToSet: "$value" 
		} 
	} 
}]);

I hit the 16MB aggregation result limitation:

{
	"errmsg" : "exception: aggregation result exceeds maximum document size (16MB)",
	"code" : 16389,
	"ok" : 0
}

MongoDB documents are limited to 16MB, and prior to the 2.6 version, the aggregation result was a BSON document. The 2.6 version replaced it with a cursor instead.

Running the same query on 2.6 yields the following result:

db.randomData.aggregate( [ 
{ 
	$match: { 
		"created_on" : { 
			$gte : new Date(Date.UTC(2012, 0, 1)), 
			$lte : new Date(Date.UTC(2012, 0, 10)) 
		} 
	} 
},  
{ 
	$group: {
		_id : {
			"minute" : {
				$minute : "$created_on"
			} 
		},  
		"values": { 
			$addToSet: "$value" 
		} 
	} 
}])
.objsLeftInBatch();
14

I used the cursor-based objsLeftInBatch method to test the aggregation result type and the 16MB limitation no longer applies to the overall result. The cursor inner results are regular BSON documents, hence they are still limited to 16MB, but this is way more manageable than the previous overall result limit.

The 2.6 version also addresses the aggregation memory restrictions. A full collection scan such as:

db.randomData.aggregate( [   
{ 
	$group: {
		_id : {
			"minute" : {
				$minute : "$created_on"
			} 
		},  
		"values": { 
			$addToSet: "$value" 
		} 
	} 
}])
.objsLeftInBatch();

can end up with the following error:

{
	"errmsg" : "exception: Exceeded memory limit for $group, but didn't allow external sort. Pass allowDiskUse:true to opt in.",
	"code" : 16945,
	"ok" : 0
}

So, we can now perform large sort operations using the allowDiskUse parameter:

db.randomData.aggregate( [   
{ 
	$group: {
		_id : {
			"minute" : {
				$minute : "$created_on"
			} 
		},  
		"values": { 
			$addToSet: "$value" 
		} 
	} 
}]
, 
{ 
	allowDiskUse : true 
})
.objsLeftInBatch();

The 2.6 version allows us to save the aggregation result to a different collection using the newly added $out stage.

db.randomData.aggregate( [ 
{ 
	$match: { 
		"created_on" : { 
			$gte : new Date(Date.UTC(2012, 0, 1)), 
			$lte : new Date(Date.UTC(2012, 0, 10)) 
		} 
	} 
},  
{ 
	$group: {
		_id : {
			"minute" : {
				$minute : "$created_on"
			} 
		},  
		"values": { 
			$addToSet: "$value" 
		} 
	} 
},
{ 
	$out : "randomAggregates" 
}
]);
db.randomAggregates.count();
60

New operators have been added such as let, map, cond, to name a few.

The next example will append AM or PM to the time info of each specific event entry.

var dataSet = db.randomData.aggregate( [ 
{ 
	$match: { 
		"created_on" : { 
			$gte : new Date(Date.UTC(2012, 0, 1)), 
			$lte : new Date(Date.UTC(2012, 0, 2)) 
		} 
	} 
},  
{ 
	$project: { 
		"clock" : { 
			$let: {
				vars: {
					"hour": { 
						$substr: ["$created_on", 11, -1]
					},				
					"am_pm": { $cond: { if: { $lt: [ {$hour : "$created_on" }, 12 ] } , then: 'AM',else: 'PM'} }
				},
				in: { $concat: [ "$$hour", " ", "$$am_pm"] }				
			}			
		}   
	} 
}, 
{
	$limit : 10
}
]);
dataSet.forEach(function(document)  {
	printjson(document);
});

Resulting in:

"clock" : "16:07:14 PM"
"clock" : "22:14:42 PM"
"clock" : "21:46:12 PM"
"clock" : "03:35:00 AM"
"clock" : "04:14:20 AM"
"clock" : "03:41:39 AM"
"clock" : "17:08:35 PM"
"clock" : "18:44:02 PM"
"clock" : "19:36:07 PM"
"clock" : "07:37:55 AM"

Conclusion

MongoDB 2.6 version comes with a lot of other enhancements such as bulk operations or index intersection. MongoDB is constantly evolving, offering a viable alternative for document-based storage. At such a development rate, there’s no wonder it was named 2013 database of the year.

Reference: MongoDB 2.6 is out from our JCG partner Vlad Mihalcea at the Vlad Mihalcea’s Blog blog.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

Leave a Reply


eight − = 2



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close