Samira Arabgol Samira Arabgol - 1 year ago 95
Javascript Question

how to use MongoDB mapReduce function in Meteor javascript file?

I am currently working on GeoSpatial project which I am using MongoDB as database and Meteor for creating my app. I have run some query(with mapReduce) in MongoDB on spatial data and I want to put this code(query) on Meteor (javascript file). I did some research on that but still i have problem. I don't know how to write mapReduce function in meteor.

here is my mapReduce code in MongoDB:

var map1 = function() {

var px =-83.215;
var py =41.53;

if(this.geometry.minlon<=px && px<=this.geometry.maxlon && this.geometry.minlat<=py && py<=this.geometry.maxlat)
{emit(this._id, 1);}


var reduce1 = function(key, value) {
return Array.sum(value)

db.C1DB.mapReduce(map1, reduce1, {
out: "CollectionName"

Tdm Tdm
Answer Source

Basically you can do it in 2 similar ways:

Here's working example how to do it without package:

C1DB = new Mongo.Collection('c1db');
CollectionName = new Mongo.Collection('CollectionName');

    doMapReduce: function () {
        var mapFn = function () {
            var px = -83.215;
            var py = 41.53;

            if (this.geometry.minlon <= px && px <= this.geometry.maxlon && this.geometry.minlat <= py && py <= this.geometry.maxlat) {
                emit(this._id, 1);

        var reduceFn = function (key, value) {
            return Array.sum(value)

        var rawC1DB = C1DB.rawCollection();

        // convert mapReduce to synchronous function
        var syncMapReduce = Meteor.wrapAsync(rawC1DB.mapReduce, rawC1DB);

        // CollectionName will be overwritten after each mapReduce call
        syncMapReduce(mapFn, reduceFn, {
            out: "CollectionName"

        return CollectionName.find({}).fetch();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download