Good morning fellow programmers!
I wondered if someone could shed some light on a question i'm having a hard time finding a clear answer for.
I have a MongoDb database for storing sports teams and fixtures, and I have 2 collections.
Fixtures:
{
_id:ObjectId("fff"),
homeTeam:ObjectId("hhh"),
awayTeam:ObjectId("aaa"),
dateOfFixture:ISODate("ddd")
}
Teams:
{
_id:ObjectId("ttt"),
name:"team1",
currentLeaguePosition: 3,
}
The truth is my data structure is far more complicated than this, but the above example is suitable for me to get my point across and hopefully find a solution.
So, I chose to reference instead of embed my teams in my fixtures, this is because there is data that will change in the team document on a regular basis.
I know that you cannot perform joins in MongoDb, but is there a way of querying mongodb and returning a document that looks like this:
{
_id:ObjectId("fff"),
homeTeam:
{
_id:ObjectId("t1"),
name:"Team 1", ...
},
awayTeam:
{
_id:ObjectId("t2"),
name:"Team 2", ...
},
dateOfFixture:ISODate("ddd")
}
using MapReduce or some other method. I have been researching MapReduce but am struggling to get my head around it, and am unsure if it is even the right tool.
My application is build using C# so i am using the MongoDB C# driver.
At the moment, the solution i have in my head is to make 2 separate calls to the database:
- Retrieve the Fixture(s)
- Retrieve the teams in those fixtures
and populate my c# objects in code to return a "complete" fixture object. I have one set of objects to represent my data structure (DTOs, I.e Fixture with Ids to teams) and I have another set of objects that hold the completed object (Fixture with the actual Team objects for home and away)
However, as i mentioned before, my data structure is far more complicated than this, so for some more complicated objects it might take 5 or 6 calls to fully populate the object in code.
Thanks for taking the time to read this, and please let me know if you require more information to paint a better picture of my problem.
And Happy Back to the Future day!