Home > database >  concat not beeing recognized by mongodb
concat not beeing recognized by mongodb

Time:08-25

I have this field on my collection:

uniqueId: 123
inTarefa: true
exclude: "ab,cd,"
orderId: 987

and I'm trying to update all values using a "FindOneAndUpdate" like this:

collection.findOneAndUpdate({
'uniqueId': 123
},
{{
  "$set":{
  "inTarefa":false,
  "orderId":0,
  "exclude":{"$concat":["$exclude","servico"]}
}}})

inTarefa and orderId is successfully changed but the "exclude" is changed to an object like this:

exclude:{$concat:[0: "$exclude", 1: "servico"]}

Yeah, this is exactly what it's supposed to do IF the keyword "$concat" was not a command to mongodb. What I'm doing wrong?

Mongodb version: 4.3.1

CodePudding user response:

$concat is an aggregation operator, you should use the pipelined form of update. Try this:

collection.findOneAndUpdate({
'uniqueId': 123
},
[
  {"$set":{
    "inTarefa":false,
    "orderId":0,
    "exclude":{"$concat":["$exclude","servico"]}
  }}
])
  • Related