[SERVER-36261] Support field projection based on string inside of field name Created: 24/Jul/18  Updated: 06/Dec/22

Status: Backlog
Project: Core Server
Component/s: Aggregation Framework
Affects Version/s: None
Fix Version/s: None

Type: New Feature Priority: Major - P3
Reporter: Aaron Kerti Assignee: Backlog - Query Optimization
Resolution: Unresolved Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
related to SERVER-11947 Add a regex expression to the aggrega... Closed
related to SERVER-267 Wildcard support in index/query/proje... Backlog
Assigned Teams:
Query Optimization
Participants:

 Description   

Suppose you have documents that look similar to this:

{ "PRE-field1" : "value",  "PRE-field2" : "value", "PRE-field3" : "value"}

Fields in a document may start with "PRE-" and have any characters after that. During the projection stage, I want to be able to project all fields that contain the string "PRE-". Maybe the project statement looks something like this:

 "$fieldNameSearch" : /.*PRE-.*/i



 Comments   
Comment by Asya Kamsky [ 03/Aug/18 ]

akerti

You can do this currently with aggregation if you can express the regular expression as simple substring match. SERVER-11947 is absolutely relevant here because then in the conditional statement below you would be able to use full regex syntax.

For simple cases like the prefix comparison with sample document you provided, you can do this:

> db.foo.find()
{ "_id" : ObjectId("5b6486405a6efe4780ae9c43"), "PRE-field1" : "value", "PRE-field2" : "value", "PRE-field3" : "value", "notPREfield" : 1 }
> db.foo.aggregate([{$replaceRoot:{newRoot:{
   $arrayToObject:{
      $filter:{
         input:{$objectToArray:"$$ROOT"},
         cond:{
            $eq:[
               "PRE-",
               {$substr:["$$this.k",0,4]}
            ]
         }
      }
   }
}}}])
{ "PRE-field1" : "value", "PRE-field2" : "value", "PRE-field3" : "value" }

This ticket is more directly related to SERVER-267 which asks for support of wildcards in queries, projections and indexes - though that's not specific to aggregation $project stage.

Comment by Nick Brewer [ 25/Jul/18 ]

akerti On looking at this closer I can see that your request is a little different, as you're trying to match against field names as opposed to values. I'm passing this along to our Query team as a feature request.

-Nick

Comment by Nick Brewer [ 25/Jul/18 ]

akerti I believe the behavior you're describing is covered as part of a larger ticket for adding enhanced regex support to the aggregation pipeline: SERVER-11947.

You can follow along with that ticket for updates on the status of this work.

-Nick

Generated at Thu Feb 08 04:42:34 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.