【问题标题】:Add more fields in MapReduce with Mongoose and NodeJS使用 Mongoose 和 NodeJS 在 MapReduce 中添加更多字段
【发布时间】:2013-12-20 00:54:24
【问题描述】:

我有这个猫鼬模式

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

我需要知道有多少文档具有不同的 ID,但我只需要那些 startDate 大于给定日期(例如今天)的文档。 运行以下代码可以正常工作,但我想在地图中添加一些字段以在查询中使用它们。

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);
                    });
                });

这是我的输出:

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

我尝试这样做:

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

....

model.find({

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

但我一直得到相同的输出。

那么如何从 map 函数中添加更多的 key-value 参数到 reduce 函数的结果字典中呢?

【问题讨论】:

  • 您是否尝试过使用聚合框架而不是 MapReduce?
  • 我也不确定最后一个查询中的 model 是什么? map reduce 集合是value.startDate
  • 对不起,我也没有找到你,型号指的是Session schema

标签: javascript node.js mongodb mongoose


【解决方案1】:

这是一个解决方案:

  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);
            });
        });

【讨论】:

  • 如果您实际上并不关心与您的日期查询不匹配的文档,您还可以定义 o.query 来预过滤要馈送到 map/reduce 函数的文档。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-05
  • 2012-03-11
  • 2020-01-20
  • 1970-01-01
  • 2011-06-08
  • 1970-01-01
相关资源
最近更新 更多