Trying to update my collection with a new field containing the value of other field are being stored as the name field

Hello guys,
I’m struggling a bit trying to update my document on Mongo 4.4.

Is pretty straightforward:

db.getCollection("cs").updateOne(
    {},
    [
        {
            $set: {
                "forwardData.clients.$[].relayedEndpoints": [{
                    type: "vehicle",
                    endpoint: "$forwardData.vehicle.path"
                }]
            }
        }
    ]
);

Running the query above, i’m facing this error:
Invalid $set :: caused by :: FieldPath field names may not start with ‘$’.

After some research, i saw some forum answers to similar problems to remove the brackets from the second argument. It doesn’t throw any errors doing this, but the value are being saved as a string with the field name instead of his value.

Could you guys tell me what i’m doing wrong? I tried with $push instead of $set too, and got the same result.

The problem was the array after the clients object…

db.getCollection("cs").updateOne(
    {},
    [{
        $set: {
            "forwardData.clients.relayedEndpoints": {
                type: "vehicleMakes",
                endpoint: "$forwardData.vehicleMakes.path"
            }
        }
    }]
);

Solved with this

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.