Add more fields in MapReduce with Mongoose and NodeJS

Viewed 1503

I've this mongoose schema

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 ids, but i only need those that have startDate greater than a given date (for example today). Running the following code works fine but i want to add some fields in map to use them in the query.

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 output :

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

I try 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 keep getting the same output.

So how do I add more key-value params from the map function to the result dictionary of the reduce function?

1 Answers
Related