spring-data-mongodb icon indicating copy to clipboard operation
spring-data-mongodb copied to clipboard

Create array from field in update pipeline

Open Saljack opened this issue 11 months ago • 0 comments

I would like to concat an array with another field. If I use normal update then I can use $push operator but in pipeline update I have to use $concatArrays. The problem is that I have to create array from a field reference but it is not possible because input string is replaced by just a field reference. For example I have document:

{
  "myArray": ["something"],
   "myField": "else"
}

and as result I want to have:

{
  "myArray": ["something", "else"]
}

I want to run this MongoDB Update pipeline:

db.getCollection("myCollection").update({}, [
    { "$set": { myArray: {"$concatArrays": ["$myArray", [ "$myField" ] ]} }
])

I tried to rewrite it to:

.set("myArray").toValue(ArrayOperators.ConcatArrays.arrayOf("myArray").concat("[\"$myField\"]"))

but it is translated to:

{ "$set" : { "myArray" : { "$concatArrays" : ["$myArray", "$myField\"]"]}}}

I would use custom AggregationExpresion but it has to return Document but this is unfortunately array.

Saljack avatar Dec 20 '24 11:12 Saljack