Map-Reduce to combine data (MongoDb)

一笑奈何 提交于 2019-12-12 07:05:48

问题


I have two collections.

  LogData
 [{
  "SId": 10,
  "NoOfDaya" : 9,
  "Status" : 4
 }
{
"SId": 11,
"NoOfDaya" : 8,
 "Status" : 2
 }]



 OptData 
 [ {
 "SId": 10,
"CId": 12,
"CreatedDate": ISO(24-10-2014)
}

 {
  "SId": 10,
  "CId": 13,
  "CreatedDate": ISO(24-10-2014)
}]

Now using mongoDB I need to find the data in form

select  a.SPID,a.CreatedDate,CID=(MAX(a.CID)) from OptData a     
Join LogData c  on a.SID=c.SID where Status>2
group by a.SPID,a.CreatedDate

LogData have 600 records whereas OPTData have 90 millions records in production. I need to update LogData frequently, that's why its in separate collection.

  1. Please don't suggest to keep data in one collection.
  2. This is same query, I asked with different approach Creating file in GridFs (MongoDb)
  3. Please don't suggest Joins can't be applied in mongoDB.

回答1:


Because MongoDB does not support JOINs, you will have to perform two separate queries and do the JOIN on the application layer. With just 600 documents the collection LogData is very small, so it should be no problem to completely load it into your applications memory and use it to enrich the results returned from OptData.

Another option would be to denormalize the data from LogData by mirroring the fields you need from LogData in the respective documents in OptData. So your OptData documents would look something like this:

{
   "SId": 10,
   "CId": 12,
   "CreatedDate": ISO(24-10-2014),
   "LogStatus": 2
}


来源:https://stackoverflow.com/questions/28714773/map-reduce-to-combine-data-mongodb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!