Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-19476

Referenced Values

    XMLWordPrintableJSON

Details

    • New Feature
    • Status: Closed
    • Minor - P4
    • Resolution: Duplicate
    • None
    • None
    • None
    • None
    • Fully Compatible

    Description

      Here is a crazy new feature idea that handles a very common problem when dealing with databases. It concerns when you have a field on Table A which is always meant to be synchronized with a field of a referenced object on Table B.

      Say you have the following tables:

      Person

      { name: String, job: ObjectID }

      Job

      { title: String, description: String }

      Each person object references the job that the person works. For various reasons, such as indexing, request performance, or convenience, you may actually want a copy of the persons job title on the Person object itself, like so:

      Person

      { name: String, job: ObjectID, jobTitle: String }

      The jobTitle field is always just copied from whatever job object is referenced. This isn't amazingly clean, and it does result in duplication of data - in the ideal world it wouldn't be necessary. But this does come up a lot in practice in real world programming.

      The main problem is keeping the jobTitle field up to date. Every time and place that we ever change the title field on the Job object, we have to ensure that we update every Person object which references that Job. This is a nightmare to maintain and these bugs are the bane of my existence.

      What I would like to see in my database is some sort of Referenced Value or Foreign Value functionality. Instead of actually setting the jobTitle to a string, I might set it to a referenced value like so:

      Person

      { name: "bob", job: ObjectId(55a7fd9c99d1c7a113000043), jobTitle: ReferencedValue("job.title") }

      Now anytime I request a Person object, I will get the correct corresponding jobTitle which is contained within the referenced Job object. If the Job doesn't exist, jobTitle might return null. And if I change the corresponding Job object, Mongo will automatically update all Person objects which reference it to have the new title.

      This might not be easy to implement, but it would save developers a hell of a lot of pain from referential integrity related issues. And its in keeping with the spirit of MongoDB, which is to be a powerful, fully featured database which makes development easier by handling more of the functionality itself.

      Attachments

        Issue Links

          Activity

            People

              sam.kleinman Sam Kleinman (Inactive)
              genixpro Bradley Arsenault
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: