1

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:

  1. Retrieve the Fixture(s)
  2. 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!

pieperu
  • 2,662
  • 3
  • 18
  • 31
  • I appreciate MongoDB does not do joins and my intention at this time (as i stated above) is to do exactly that, make multiple calls in my application to compile my data objects, it just seems a bit painful. I saw an example in the linked question you post above, where someone was executing a foreach in mongo shell and inserting a compiled document into a separate collection. What do you think of this solution? Do you see any benefit from doing this vs doing it in the application itself? – pieperu Oct 21 '15 at 12:31
  • Why not use a relationship database – Alex Krupka Oct 21 '15 at 18:01
  • This is an option for the core parts of the system, like Fixtures and Teams, however some of the other parts of the system will have millions of records and each document could potentially have different fields, hence why a NoSql database is being used. It will also mean having an entity in an RDBMS, and an equivalent in MongoDb, Do you have any experience with this approach? – pieperu Oct 22 '15 at 08:59
  • I decided to use Redis cache to cache all of my complete domain models. My problem was the fact that I was making many many calls to MongoDb and was suffering from the performance implications of this. Normally I would cache in the DAL directly, however, I have found caching one level up for MongoDb to be very beneficial in terms of performance. – pieperu Oct 29 '15 at 16:17

0 Answers0