0

I am trying to create join in mongodb collection.

My collection is LogScheduler

{
"_id" : ObjectId("54dc5181e043683f3f03f87b"),
"ScheduleLogId" : 10,
"ScheduleId" : 9,
"SubscriberPropertyId" : 47984,
"SubscriberPropertyName" : "NA",
"StartDateTime" : "[2014/09/24:12:00:00 AM]",
"DaysOfData" : 125,
"Status" : 4,
"CreatedDate" : "[2014/09/24:12:33:00 PM]",
"ModifiedDate" : "[2014/09/24:12:33:11 PM]"
}

2nd collection OptimisationReport

{
"_id" : ObjectId("54dc51e2e043683f3f03fa9e"),
"OptimisationId" : 1,
"ScheduleLogId" : 10,
"ReportId" : 4113235,
"SubscriberPropertyId" : 10038,
"PropertyId" : 18166,
"ChannelId" : 701,
"CheckInDate" : "[2014/10/01]",
"LengthOfStay" : 1
}

based on OptimisationReport.ScheduleLogId=LogScheduler.ScheduleLogId, my new collection should be created e.g

newOptimisationReport{
 ....OptimisationReport data....
 //since OptimisationReport.ScheduleLogId = 10
//since LogScheduler.ScheduleLogId = 10
// ADD LogScheduler data where LogScheduler.ScheduleLogId = 10
LogScheduler {

            ....logscheduler data....

}
}

I tried something like this

db.Subscription_OptimisationReports.find().forEach(
function (newSubscription_OptimisationReports) {
    newSubscription_OptimisationReports.ScheduleLogId = db.Log_Scheduler.findOne( {"ScheduleLogId": newSubscription_OptimisationReports.ScheduleLogId} );
    db.newSubscription_OptimisationReports.insert(newSubscription_OptimisationReports);
}
);

but its not comparing the id's

sangita
  • 191
  • 5
  • 14
  • MongoDB does not have joins - see the documentation for modelling relationships http://docs.mongodb.org/manual/applications/data-models-relationships/#model-relationships-between-documents – Martin Feb 16 '15 at 17:53
  • Btw. your dates are strings. Converting them to proper ISODate()'s will make querying them much easier – Martin Feb 16 '15 at 17:53
  • please do not divert the question. I know mongodb dont have joins but you can create single collection. please see last example http://stackoverflow.com/questions/2350495/how-do-i-perform-the-sql-join-equivalent-in-mongodb. This is similar I am trying to. – sangita Feb 16 '15 at 18:18
  • 1
    You need to write some application code to first retrieve data from the first collection, then the second collection, join them and then persist in the third collection or use `Map-reduce`. Once you have tried either of the solution, you could post it here for any further help. – BatScream Feb 16 '15 at 18:55
  • 2
    @sangita I'm not asking your question, but it was unclear what your were asking until you added your comment. As BatScream mentioned, write some code reading the two collections and save the result in a new collection - just like the answers in the question you linked to – Martin Feb 16 '15 at 19:27
  • Remove the `{` and `}` braces from around `newSubscription_OptimisationReports.ScheduleLogId` in the `findOne` call. The way it is shouldn't even run as the syntax is invalid. – JohnnyHK Feb 17 '15 at 04:43
  • Correct, I removed it. Any solution to real problem? – sangita Feb 17 '15 at 05:15
  • You removed the wrong braces, the parameter to `findOne` should be `{"ScheduleLogId": newSubscription_OptimisationReports.ScheduleLogId}`. I don't understand, aren't you getting SyntaxError exceptions trying to run this? – JohnnyHK Feb 17 '15 at 13:04

0 Answers0