Skip to content

Create array from field in update pipeline #4861

Open
@Saljack

Description

@Saljack

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.

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions