问题
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.
- Please don't suggest to keep data in one collection.
- This is same query, I asked with different approach Creating file in GridFs (MongoDb)
- 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