[SERVER-15697] Create a string accumulator for aggregation Created: 16/Oct/14  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: GERVAIS Mickaël Assignee: Backlog - Query Execution
Resolution: Unresolved Votes: 1
Labels: accumulator, expression, pm1457-nominee
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends
Related
is related to SERVER-17258 Add $reduce expression operator for r... Closed
is related to SERVER-41346 all accumulators should be available ... Closed
Assigned Teams:
Query Execution
Participants:

 Description   

Hi,

I'd like to join all elements in an array in a aggregation.
A string accumulator, should do the job. Is it plannes to create one?

Thanks



 Comments   
Comment by Asya Kamsky [ 11/Aug/21 ]

This can also track expression for "join" (like JS or Python join for strings with optional join character).

 

Comment by Asya Kamsky [ 13/Sep/17 ]

Leaving the ticket open to track whether we want to create a $group accumulator "$concat".

Comment by Asya Kamsky [ 13/Sep/17 ]

As of 3.4 it's possible to do this via $reduce something like this:

db.c.aggregate({$addFields:{bAsString:{$reduce:{
        input:"$b", 
        initialValue:"", 
        in:{$concat:["$$value","$$this"]}
}}}})
{ "_id" : 1, "b" : [ "a", "b", "c" ], "bAsString" : "abc" }
// with delimiter 
db.c.aggregate({$addFields:{bAsString:{$reduce:{
        input:{$slice:["$b",1,{$size:"$b"}]}, 
        initialValue:{$arrayElemAt:["$b",0]}, 
        in:{$concat:["$$value","-","$$this"]}
}}}})
{ "_id" : 1, "b" : [ "a", "b", "c" ], "bAsString" : "a-b-c" }

Comment by Asya Kamsky [ 02/Aug/15 ]

Suggest we use this ticket to track operator equivalent to concat strings in array. It's possible that if SERVER-17258 is implemented first, this one will become syntactic nicety (like $concatArrays, $avg, $sum(Arrays) accumulators for arrays in $project.

Comment by Ramon Fernandez Marina [ 21/Oct/14 ]

Thanks for following up mgervais, we've marked this ticket to be considered for future improvements in the aggregation framework.

Comment by GERVAIS Mickaël [ 21/Oct/14 ]

Hi,

Thanks for your responses.

Asya has given a good example. I've an array of string, and I need to concatenate values with a separator (or not) to generate a key and check if this key exists in a map of rules.

In Java lambda, it's possible to reduce values by concatenating them.

Have I been clear enough ?

Comment by Asya Kamsky [ 18/Oct/14 ]

I can see it being useful - if you have something like:

{ _id:1, b: [ "a", "b", "c" ] }
db.collection.aggregate({$unwind:"$b"},  {$group:{_id:"$_id", bstring:{$concat:"$b"}}})
{ _id:1, bstring: "abc" }

I've made an assumption that "$concat" would work the way "$sum" does currently, though given that we use $sum and not $add maybe it makes sense for concatenating operator to be named something different from the project operator.

Comment by Ramon Fernandez Marina [ 17/Oct/14 ]

mgervais, can you provide a more detailed example of the kind of data you have and how would you like to manipulate it with this new hypothetical aggregation operator? Sample input and output would help clarify what is it you need, and whether there are alternative ways to do it.

Thanks,
Ramón.

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