$multiply only supports numeric types, not string

Hi MongoDB community,
I’m pretty new to mongoDB, so let me apology first for any silly issues I’ll raise to you.

I’m trying to add a ‘calculated’ field (field13 = field1 * field3) into an array (Array) of documents.
That’s the final document I’m trying to get:

    "field13": field1*field3

To do so, I’m using $addfields and $multiply in an aggregation stage:

[{$addFields: {"Array.field13": {$multiply:["$Array.0.field3","$field0"]}}}]

Despite many attempts, I always get the error: $multiply only supports numeric types, not array. On the contrary, if I use

[{$addFields: {"Array.field13": {$multiply:["$field1,"$field0"]}}}]
everything works fine.

What am I doing wrong in referring to the value of field3?

Thank you in advance for any help.


You cannot reference array elements by “.x” type syntax - you need to use $arrayElemAt expression.

1 Like

Hi Aysa,

Finally, I got it!

$addFields: {"Array.field13": {$multiply:[{$arrayElemAt:["$Array.field3",0]},"$field0"]}}



1 Like

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