我需要 $multiply 数组中的两个元素,但问题是其中一个元素是一个字符串,所以我怎样才能将它更改为整数
I need to $multiply two elements in an array, but the problem is one of the element is a string, so how can i change it to an integer
**`{
$project: {
item:1,quantity:1, product: { $arrayElemAt: ['$products.price',0] }
}
},
{
$project: {
item:1,quantity:1, total : {$multiply:['$product','$quantity']}
}
}`**
这里的$product
是一串数字,$quantity
是一个整数
我尝试使用 parseInt('$product')
并且输出是:
{
_id: new ObjectId("626030ccb52876d4dd779dac"),
item: new ObjectId("6260352755ed2b52b2ff13a0"),
quantity: 3,
total: **NaN**
}
使用**$toInt**
,我们可以改变它:
{
$project: {
item:1,quantity:1, product: { $toInt :{ $arrayElemAt: ['$products.price',0] }}
}
},
{
$project: {
item:1,quantity:1, total : {$multiply:['$product','$quantity']}
}
}
**`{
$project: {
item:1,quantity:1, product: { $arrayElemAt: ['$products.price',0] }
}
},
{
$project: {
item:1,quantity:1, total : {$multiply:['$product','$quantity']}
}
}`**
这里的$product
是一串数字,$quantity
是一个整数
我尝试使用 parseInt('$product')
并且输出是:
{
_id: new ObjectId("626030ccb52876d4dd779dac"),
item: new ObjectId("6260352755ed2b52b2ff13a0"),
quantity: 3,
total: **NaN**
}
使用**$toInt**
,我们可以改变它:
{
$project: {
item:1,quantity:1, product: { $toInt :{ $arrayElemAt: ['$products.price',0] }}
}
},
{
$project: {
item:1,quantity:1, total : {$multiply:['$product','$quantity']}
}
}