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

Ability to make use of a subdocument's data whose contents were used to satisfy a query using the $ operator

    XMLWordPrintable

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Critical - P2
    • Resolution: Duplicate
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: Querying
    • Labels:
      None

      Description

      When performing a query against indexed subdocument fields we are able to retrieve and update the subdocument within an array using the $ operator. There are cases where we would have need to know what subdocuments were located who satisfied our query and make use of that data.

      A simple example would be to locate a document based on its subdocuments content and append a new subdocument that derives from that located subdocument...Given:

      { "SUBS" : [

      { "_id" : 1, "VALUE" : 5 }

      ], "_id" : 1 }

      Would like to execute something like:

      db.col.update(

      { "SUBS._id" : 1 }

      , { $push : { "SUBS" :

      { "_id" : 3, "VALUE" : SUBS.$.VALUE * 5 }

      } } )

      That would result in appending a new subdocument "_id":3 in all documents who contain the matched subdocument from our query with a new value derived from the matched subdocument:

      { "SUBS" : [

      { "_id" : 1, "VALUE" : 5 }

      ,

      { "_id" : 3, "VALUE" : 25 }

      ], "_id" : 1 }

      Please see (http://groups.google.com/group/mongodb-user/browse_thread/thread/7f0409356557aa64/468de81a1f938046?lnk=gst&q=serenityexperience#468de81a1f938046) for full discussion.

      This relates to https://jira.mongodb.org/browse/SERVER-831, but in that case the subdocuments are being updated. This request is for the ability to access the subdocuments content who satisfied the query so we can make further decisions/modifications based on that content.

      As an added advantage I could seen a lot of power added to JavaScripting if this were also available within JS, for example:

      function( previousID, newID )
      {
      db.col.find(

      { "SUBS._id" : previousID }

      ).forEach(
      function( thing )
      {
      previousValue = thing.SUBS.$.VALUE;
      db.col.update(

      { "_id" : thing._id }

      ,
      { $push : { "SUBS" :

      { "_id" : newID, "VALUE" : previousValue * 5 }

      } } );
      } );
      }

      In this case, where the primary document was queried based on its subdocument contents, it would be significant if we had access to that data through the passed in matched Document. I am not sure if this would be the ideal syntax but having some way to reference the specific subdocument that satisfied the this outer query can give a lot of power to the inner for making decisions based on such content.

        Attachments

          Issue Links

            Activity

              People

              • Votes:
                24 Vote for this issue
                Watchers:
                22 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: