挑战:从输入元素聚合第二个元素

时间:2014-06-28 21:19:24

标签: javascript node.js mongodb mapreduce aggregation-framework

对于MongoDB来说,这是一个我感到有趣的挑战。

给定带时间戳的集合events和特定的输入选择器_object,我们如何汇总跟随输入的event个文档列表?

例如,Mongoose中的Schema:

var EventSchema = new Schema({
    _object:   { type: ObjectId }
  , timestamp: { type: Date, default: Date.now }
});

示例集合:

[
    { _id: ObjectId('1'),  _object: ObjectId('123abc...1', timestamp: 'Sat Jun 21 2014 16:30:01 GMT-0400 (EDT) ) }
  , { _id: ObjectId('2'),  _object: ObjectId('123abc...2', timestamp: 'Sat Jun 22 2014 16:30:00 GMT-0400 (EDT) ) }
  , { _id: ObjectId('3'),  _object: ObjectId('123abc...1', timestamp: 'Sat Jun 23 2014 16:30:01 GMT-0400 (EDT) ) }
  , { _id: ObjectId('4'),  _object: ObjectId('123abc...3', timestamp: 'Sat Jun 24 2014 16:30:01 GMT-0400 (EDT) ) }
  , { _id: ObjectId('5'),  _object: ObjectId('123abc...1', timestamp: 'Sat Jun 25 2014 16:30:01 GMT-0400 (EDT) ) }
  , { _id: ObjectId('6'),  _object: ObjectId('123abc...4', timestamp: 'Sat Jun 26 2014 16:30:02 GMT-0400 (EDT) ) }
  , { _id: ObjectId('7'),  _object: ObjectId('123abc...1', timestamp: 'Sat Jun 27 2014 16:30:00 GMT-0400 (EDT) ) }
  , { _id: ObjectId('8'),  _object: ObjectId('123abc...3', timestamp: 'Sat Jun 28 2014 16:30:01 GMT-0400 (EDT) ) }
  , { _id: ObjectId('9'),  _object: ObjectId('124abc...1', timestamp: 'Sat Jun 29 2014 16:30:00 GMT-0400 (EDT) ) }
  , { _id: ObjectId('10'), _object: ObjectId('124abc...2', timestamp: 'Sat Jun 30 2014 16:30:00 GMT-0400 (EDT) ) }
]

让我们说我们的目标是ObjectId('123abc...1')。我们使用我们的特殊方法查询我们的集合,提供序列号1的参数(而不是0,元素本身):

Events.mySpecialMethod( { _object: ObjectId('123abc...1') } , 1 ).exec(function(err, output) {
  console.log(output); // contains intended results (see below)
});

此类查询的预期输出是:

[
    { _id: ObjectId('2'),  _object: ObjectId('123abc...2', timestamp: 'Sat Jun 22 2014 16:30:00 GMT-0400 (EDT) ) }
  , { _id: ObjectId('4'),  _object: ObjectId('123abc...3', timestamp: 'Sat Jun 24 2014 16:30:01 GMT-0400 (EDT) ) }
  , { _id: ObjectId('6'),  _object: ObjectId('123abc...4', timestamp: 'Sat Jun 26 2014 16:30:02 GMT-0400 (EDT) ) }
  , { _id: ObjectId('8'),  _object: ObjectId('123abc...3', timestamp: 'Sat Jun 28 2014 16:30:01 GMT-0400 (EDT) ) }
]

在这种情况下,选择我们预期结果集的第一个元素很简单:

Event.find({ _object: ObjectId('123abc...1' }).limit(2).exec(function(err, events) {
  // select the _second_ element of our result set
  console.log(events[1];);
});

...但是如何在给定输入_object的情况下聚合第二个元素的列表,这可能有很多条目?

加分:我们可以选择* n * th元素吗?

1 个答案:

答案 0 :(得分:1)

不确定这是否是这样的最佳用法,但你似乎想要某种方式跳过每一个游标结果。真的,这可能是你应该做的,尽管这当然意味着实际上检索所有结果,即使你丢弃了你不想要的那些。

如果您真的坚持尝试让服务器执行此操作,那么可能的方法是使用mapReduce的JavaScript评估为您执行此操作。

考虑样本:

{ _id: 1, oth: "A", grp: "A" },
{ _id: 2, oth: "B", grp: "A" },
{ _id: 3, oth: "C", grp: "A" },
{ _id: 4, oth: "D", grp: "A" },
{ _id: 5, oth: "E", grp: "B" },
{ _id: 6, oth: "F", grp: "B" },
{ _id: 7, oth: "G", grp: "B" },
{ _id: 8, oth: "H", grp: "B" }

为了得到每一秒或nth项,你基本上都是模数:

db.sequence.mapReduce(
    function () {
        counter++;
        var id = this._id;
        delete this._id;
        if ( counter % seq == 0 )
          emit( id, this );
    },
    function() {}, // blank mapper
    {
        "scope": { "counter": 0, "seq": 2 },
        "out": { "inline": 1 }
    }
)

给你这样的结果:

{ "_id" : 2, "value" : { "oth" : "B", "grp" : "A"  } },
{ "_id" : 4, "value" : { "oth" : "D", "grp" : "A"  } },
{ "_id" : 6, "value" : { "oth" : "F", "grp" : "B"  } },
{ "_id" : 8, "value" : { "oth" : "H", "grp" : "B"  } }

如果您希望从起始位置查询,那么您只需发出一个带有

的查询部分
db.sequence.mapReduce(
    function () {
        counter++;
        var id = this._id;
        delete this._id;
        if ( counter % seq == 0 )
          emit( id, this );
    },
    function() {}, // blank mapper
    {
        "query": { "oth": "B" },
        "scope": { "counter": 0, "seq": 2 },
        "out": { "inline": 1 }
    }
)

然后你就是从那个位置开始工作:

{ "_id" : 3, "value" : { "oth" : "C", "grp" : "A" } },
{ "_id" : 5, "value" : { "oth" : "E", "grp" : "B" } },
{ "_id" : 7, "value" : { "oth" : "G", "grp" : "B" } }

Map-reduce始终按发出的_id键对结果进行排序。这是设计意图,以确保事情按顺序“减少”。但是你可以使用该值来影响你的结果,你也可以对输入进行“排序”:

db.sequence.mapReduce(
    function () {
        counter++;
        var id = this._id;
        delete this._id;
        if ( counter % seq == 0 )
          emit( -id, this );
    },
    function() {}, // blank mapper
    {
        "sort": { "oth": -1 },
        "scope": { "counter": 0, "seq": 2 },
        "out": { "inline": 1 }
    }
)

因此,通过使发出的键为负值来向后计数并对输出进行排序:

{ "_id" : -7, "value" : { "oth" : "G", "grp" : "B" } },
{ "_id" : -5, "value" : { "oth" : "E", "grp" : "B" } },
{ "_id" : -3, "value" : { "oth" : "C", "grp" : "A" } },
{ "_id" : -1, "value" : { "oth" : "A", "grp" : "A" } }

为了以其他方式“跳过”到选择点,您可以更改逻辑

db.sequence.mapReduce(
    function () {
        counter++;
        var id = this._id;
        delete this._id;
        if ( counter % seq == 0 )
            seen++;

        if ( seen == skip && counter % seq == 0 )
            emit( id, this );
    },
    function() {}, // blank mapper
    {
        "scope": { "counter": 0, "seq": 2, "seen": 0, "skip": 3 },
        "out": { "inline": 1 }
    }
)

这将带来第二个序列:

{ "_id" : 6, "value" : { "oth" : "F", "grp" : "B"  } }

请记住,所有这些“扫描”所有对输入查询有效的结果,因此您只是在服务器端而不是客户端上“跳过”光标。