我有一个mongo集合。使用node.js驱动程序:
db.collection( '通知')找到();
返回:
{ _id: '1376179201714-user102',
state: 'unread',
sender: 'Daily Notify Process',
datereceived: 1376179201714,
subject: 'Training is past due!',
message: 'Training3 is past due!!!',
recipient: [ 'test@test.com' ] }
{ _id: '1376179201714-user103',
state: 'unread',
sender: 'Daily Notify Process',
datereceived: 1376179201714,
subject: 'training is past due!',
message: 'Training2 is past due!!!',
recipient: [ 'test@test.com' ] }
{ _id: '1376179201714-user104',
state: 'unread',
sender: 'Daily Notify Process',
datereceived: 1376179201714,
subject: 'training is past due!',
message: 'Training5 is past due!!!',
recipient: [ 'test@test.com' ] }
{ _id: '1376179201714-user133',
state: 'unread',
sender: 'Daily Notify Process',
datereceived: 1376179201714,
subject: 'training is past due!',
message: 'Training1 is past due!!!',
recipient: [ 'test2@test.com' ] }
{ _id: '1376179201714-user134',
state: 'unread',
sender: 'Daily Notify Process',
datereceived: 1376179201714,
subject: 'training is past due!',
message: 'Training2 is past due!!!',
recipient: [ 'test2@test.com' ] }
{ _id: '1376179201714-user135',
state: 'unread',
sender: 'Daily Notify Process',
datereceived: 1376179201714,
subject: 'training is past due!',
message: 'Training4 is past due!!!',
recipient: [ 'test2@test.com' ] }
{ _id: '1376179201714-user203',
state: 'unread',
sender: 'Daily Notify Process',
datereceived: 1376179201714,
subject: 'training is past due!',
message: 'Training1 is past due!!!',
recipient: [ 'test3@test.com' ] }
{ _id: '1376179201714-user204',
state: 'unread',
sender: 'Daily Notify Process',
datereceived: 1376179201714,
subject: 'Training2 is past due!',
message: 'Card maker, updating member info is past due!!!',
recipient: [ 'test3@test.com' ] }
{ _id: '1376179201714-user205',
state: 'unread',
sender: 'Daily Notify Process',
datereceived: 1376179201714,
subject: 'training is past due!',
message: 'Training3 is past due!!!',
recipient: [ 'test3@test.com' ] }
我可以使用mapreduce或group得到它:
{recipient: ['test@test.com'],
subject: 'Training is past due!',
trainingblock: [
{message: 'Training3 is past due!!!'},
{message: 'Training2 is past due!!!'},
{message: 'Training5 is past due!!!'}
]
}
{recipient: ['test@test.com'],
subject: 'Training is past due!',
trainingblock: [
{message: 'Training1 is past due!!!'},
{message: 'Training2 is past due!!!'},
{message: 'Training4 is past due!!!'}
]
}
{recipient: ['test@test.com'],
subject: 'Training is past due!',
trainingblock: [
{message: 'Training1 is past due!!!'},
{message: 'Training2 is past due!!!'},
{message: 'Training3 is past due!!!'}
]
}
或者我是否需要在javascript / node端进行此操作?
答案 0 :(得分:0)
这与我在没有花费大量时间的情况下一样接近。我不知道所有要求或数据变化的程度。例如,收件人是否总是单个值,即使它被包装在一个数组中?无论如何,它应该足够接近让你完全接受。
它由收件人和主题分组,只是发送消息。 reduce函数创建容器,对于传入的每条消息,将值推送到trainingblock。
db.collection.mapReduce(function() {
var recipient = this.recipient[0];
emit(recipient + "#" + this.subject, { message: this.message });
}, function(key, values) {
var parts = key.split('#'),
recipient = parts[0],
subject = parts[1],
block = { recipient: recipient, subject: subject, trainingblock: [] };
for(var i = 0; i < values.length; i++) {
block.trainingblock.push({ message: values[i].message });
}
return block;
}, {out: { inline: 1 }});