我有一个mongo集合。使用node.js驱动程序:
db.collection('notify').find()
退货:
{ _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/节点端执行此操作?
这是我在没有花费大量时间的情况下得到的最接近的结果。我不知道所有的要求,也不知道你的数据有多大变化。例如,即使接收者被封装在数组中,它也总是一个值吗?无论如何,它应该足够近,让你走完剩下的路。
它是按照收件人和主题进行分组,然后一起发送消息。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 }});