这是我发现有趣的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*个元素吗?
不确定这是不是最好的用法,但您似乎想要跳过每个单个游标结果。真的,这可能是你应该做的,即使这当然意味着实际上检索所有的结果,即使你丢弃了那些你不想要的。
如果你真的坚持让服务器这样做,那么一个可能的方法是使用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" } }
请记住,所有这些"扫描"对输入查询有效的所有结果,因此您只是在服务器端而不是在客户端"跳过"光标。