[SERVER-3089] Ability to make use of a subdocument's data whose contents were used to satisfy a query using the $ operator Created: 13/May/11  Updated: 06/Dec/22  Resolved: 11/Aug/17

Status: Closed
Project: Core Server
Component/s: Querying
Affects Version/s: None
Fix Version/s: None

Type: Improvement Priority: Critical - P2
Reporter: jp Assignee: Backlog - Query Team (Inactive)
Resolution: Duplicate Votes: 24
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends
depends on SERVER-831 Positional Operator Matching Nested A... Closed
Duplicate
duplicates SERVER-11345 Allow update to compute expressions u... Closed
Related
related to SERVER-828 Support for selecting array elements ... Closed
Assigned Teams:
Query
Participants:

 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.



 Comments   
Comment by Asya Kamsky [ 15/May/17 ]

I don't think this ticket is asking for anything that SERVER-11345 wouldn't give it - fundamentally, you cannot currently refer to any value in the document for the purposes of querying or updating the document. Related tickets: SERVER-2702, SERVER-2507 and SERVER-2549

Comment by Scott Hernandez (Inactive) [ 14/May/11 ]

I believe the other piece you want will be supported by http://jira.mongodb.org/browse/SERVER-828

Generated at Thu Feb 08 03:02:02 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.