Discussion:
Issues using $multiply with embedded documents
James Crosswell
2013-06-17 10:53:16 UTC
Permalink
I can run this query against the aggregation framework:

{
$group :{
_id : {"_id" : "$exists"},
quantity: {$sum: "$Quantity"},
sales: {$sum: { $multiply : ["$Quantity", "$Quantity" ] }}
}
}


But not this one:

{
$group :{
_id : {"_id" : "$exists"},
quantity: {$sum: "$Quantity"},
sales: {$sum: { $multiply : ["$Quantity", "$UnitPrice.Amount" ] }}
}
}


The only difference is the second argument to the multiply function... in
the second query this is a field of an embedded document.

Is this a bug or do I have to use a special syntax to work with embedded
fields?

Cheers,
James
--
--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongodb-user-/***@public.gmane.org
To unsubscribe from this group, send email to
mongodb-user+unsubscribe-/***@public.gmane.org
See also the IRC channel -- freenode.net#mongodb

---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
For more options, visit https://groups.google.com/groups/opt_out.
Asya Kamsky
2013-06-17 15:12:58 UTC
Permalink
Both work fine for me. Could you show the exact error you are getting
(maybe cut-and-paste it along with the exact pipeline you are entering?)

Asya
Post by James Crosswell
{
$group :{
_id : {"_id" : "$exists"},
quantity: {$sum: "$Quantity"},
sales: {$sum: { $multiply : ["$Quantity", "$Quantity" ] }}
}
}
{
$group :{
_id : {"_id" : "$exists"},
quantity: {$sum: "$Quantity"},
sales: {$sum: { $multiply : ["$Quantity", "$UnitPrice.Amount" ] }}
}
}
The only difference is the second argument to the multiply function... in
the second query this is a field of an embedded document.
Is this a bug or do I have to use a special syntax to work with embedded
fields?
Cheers,
James
--
--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongodb-user-/***@public.gmane.org
To unsubscribe from this group, send email to
mongodb-user+unsubscribe-/***@public.gmane.org
See also the IRC channel -- freenode.net#mongodb

---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
For more options, visit https://groups.google.com/groups/opt_out.
James Crosswell
2013-06-17 20:23:47 UTC
Permalink
So it turns out the issue had nothing to do with the $multiply operation.
I'm getting at Mongo from c# and the UnitPrice.Amount field on the c# POCO
was a decimal (that's the recommended data type for dealing with currencies
in dotnet)... but Mongo can't handle decimal (128 bit) precision so it
encodes decimals as strings - which makes it impossible to perform
arithmetic operations on these. For the time being, I've worked around the
problem by storing UnitPrice.Amount as a double instead of a decimal. I
think that should still be enough precision for my use case... it's a bit
of a quirk of the c# driver perhaps though.

Cheers,
James
Post by Asya Kamsky
Both work fine for me. Could you show the exact error you are getting
(maybe cut-and-paste it along with the exact pipeline you are entering?)
Asya
Post by James Crosswell
{
$group :{
_id : {"_id" : "$exists"},
quantity: {$sum: "$Quantity"},
sales: {$sum: { $multiply : ["$Quantity", "$Quantity" ] }}
}
}
{
$group :{
_id : {"_id" : "$exists"},
quantity: {$sum: "$Quantity"},
sales: {$sum: { $multiply : ["$Quantity", "$UnitPrice.Amount" ] }}
}
}
The only difference is the second argument to the multiply function... in
the second query this is a field of an embedded document.
Is this a bug or do I have to use a special syntax to work with embedded
fields?
Cheers,
James
--
--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongodb-user-/***@public.gmane.org
To unsubscribe from this group, send email to
mongodb-user+unsubscribe-/***@public.gmane.org
See also the IRC channel -- freenode.net#mongodb

---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
For more options, visit https://groups.google.com/groups/opt_out.
Loading...