Harry Marr

MongoEngine 0.3 Released

I just released version 0.3 of MongoEngine, here’s a quick breakdown of some of the main changes.

MapReduce Support

Thanks to the great work by Matt Dennewitz, we now have support for MapReduce. Here’s an example to show how it works, in which we generate frequencies of tags over a collection of blog posts:

class BlogPost(Document):
    title = StringField()
    tags = ListField(StringField())

BlogPost(title="Post #1", tags=['music', 'film', 'print']).save()
BlogPost(title="Post #2", tags=['music', 'film']).save()
BlogPost(title="Post #3", tags=['film', 'photography']).save()

map_f = """
    function() {
        this.tags.forEach(function(tag) {
            emit(tag, 1);
        });
    }
"""

reduce_f = """
    function(key, values) {
        var total = 0;
        for(var i=0; i<values.length; i++) {
            total += values[i];
        }
        return total;
    }
"""

# run a map/reduce operation spanning all posts
for result in BlogPost.objects.map_reduce(map_f, reduce_f):
    print '%s: %s' % (result.key, result.value)

# output:
# film: 3.0
# music: 2.0
# photography: 1.0
# print: 1.0

If the keys in the results correspond to _ids in the collection, you can access the relevant object by using result.object, which is lazily loaded.

New Fields

MongoEngine 0.3 sees the introduction of five new field types:

New QuerySet Methods

String-Matching Query Operators

Six new query operators have been added: contains, startswith, endswith, and their case-insensitive variants, icontains, istartswith and iendswith. These are are just shortcuts for regular expression queries.

Other Fixes and Improvements

…and much more. For full details, see the changelog.