-
Type: Improvement
-
Resolution: Duplicate
-
Priority: Critical - P2
-
None
-
Affects Version/s: None
-
Component/s: Querying
-
None
-
Query
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" : 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(
).forEach(
function( thing )
{
previousValue = thing.SUBS.$.VALUE;
db.col.update(
,
{ $push : { "SUBS" :
} } );
} );
}
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.
- depends on
-
SERVER-831 Positional Operator Matching Nested Arrays
- Closed
- duplicates
-
SERVER-11345 Allow update to compute expressions using referenced fields like Aggregation Framework's $project
- Closed
- related to
-
SERVER-828 Support for selecting array elements in return specifier (projection)
- Closed