Add more fields to MapReduce with Mongoose and NodeJS

I have this mongoose pattern

var SessionSchema = mongoose.Schema({
    id: Number,
    cure: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Cure'
    },
    performances: Array,
    startDate: Date,
    endDate: Date,
    validatee: Boolean, 
    deleted: Boolean
});

I need to know how many documents have different identifiers, but I only need those that have startDatemore than a given date (for example, today). The execution of the following code works fine, but I want to add some fields on the map in order to use them in the request.

var o = {};
                o.map = function () {
                    emit(this.id, 1
                        // list other fields like above to select them
                    )
                }
                o.reduce = function (k, vals) {
                    return vals.length
                }
                o.out = {
                    replace: 'createdCollectionNameForResults'
                };
                o.verbose = true;

                Session.mapReduce(o, function (err, model, stats) {
                    console.log('map reduce took %d ms', stats.processtime)
                    console.log("MapReduce" + JSON.stringify(model));

                    model.find().exec(function (err, docs) {
                        console.log(docs);
                    });
                });

This is my conclusion:

[ { _id: 0, value: 2 },
  { _id: 1, value: 4 },
  { _id: 2, value: 2 } ]

I am trying to do this:

....
     o.map = function () {
                        emit(this.id, {
                            startDate: this.startDate
                        })
                    }

....

model.find({

     startDate: {
                "$gte": new Date()
                }
     }).exec(function (err, docs) {
                        console.log(docs);
                    });
....

but I get the same result.

So, how can I add additional key value parameters from the map function to the dictionary of results of the reduction function?

+4
source share
1 answer

This solution:

  var o = {};
                o.map = function () {
                    emit(this.id, {
                        startDate: this.startDate,
                        cure: this.cure,
                        test: 'test'
                        // list other fields like above to select them
                    })
                }
                o.reduce = function (k, vals) {
                    return {
                        n: vals.length,
                        startDate: vals[0].startDate,
                        cure: vals[0].cure,
                        test: vals[0].test
                    }
                }
                o.out = {
                    replace: 'createdCollectionNameForResults'
                };
                o.verbose = true;



     Session.mapReduce(o, function (err, model, stats) {
            console.log('map reduce took %d ms', stats.processtime);
            model.find({
                'value.cure':mongoose.Types.ObjectId('52aedc805871871a32000004'),
                'value.startDate': {
                    "$gte": new Date()
                }

            }).exec(function (err, docs) {
                 if(!err)
                 console.log(docs.length);
            });
        });
+1
source

All Articles