'$multiply query not working in Document DB
We have created a documentDb instance having a 4.0.0 version. When running the query using $muliply it throws an error as Bad Query.
Query: 'test': { $multiply: "$price", 8 }
When checking the documentation it says the $muliply is supported. Not sure why it throws a Bad Query
Solution 1:[1]
There should be square braces encasing the field and the multiplier.
'test': { $multiply: ["$price", 8 ]}
If you still get an error, it might be that you are You have to make sure that price is not stored as string. You can use $toInt to change it to an integer or $toDouble if you are expecting decimals.
'test': { $multiply: [{$toInt:"$price"}, 8 ]}
Solution 2:[2]
Something is wrong in your code, $multiply works as expected in Amazon DocumentDB:
rs0:PRIMARY> db.collection.find().pretty()
{
"_id" : ObjectId("62693003512474f1017b6ce2"),
"name" : "prod1",
"price" : 10
}
{
"_id" : ObjectId("62693009512474f1017b6ce3"),
"name" : "prod2",
"price" : 20
}
rs0:PRIMARY>
rs0:PRIMARY> db.collection.aggregate(
... [
... { $project: { _id:0, total: { $multiply: [ "$price", 8 ] } } }
... ]
... )
{ "total" : 80 }
{ "total" : 160 }
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Ntwanano |
Solution 2 | Mihai A |