简体   繁体   English

使用Mongoose和NodeJS在MapReduce中添加更多字段

[英]Add more fields in MapReduce with Mongoose and NodeJS

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). 我需要知道有多少文档有不同的ID,但我只需要那些startDate大于给定日期的文件(例如今天)。 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? 那么如何将map函数中的更多键值参数添加到reduce函数的结果字典中呢?

This is a 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);
            });
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM