Skip to content

Instantly share code, notes, and snippets.

@bpartridge83
Created March 12, 2012 21:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bpartridge83/2024712 to your computer and use it in GitHub Desktop.
Save bpartridge83/2024712 to your computer and use it in GitHub Desktop.
http://www.mongodb.org/display/DOCS/Aggregation+Framework+-+Expression+Reference#AggregationFramework-ExpressionReference-Composition
$add:[5, $divide:[12, 2], 7]
With expressions, I can make it work properly with the nested operations enclosed in their own object:
$add:[5, { $divide:[12, 2] }, 7]
/* --------- Test --------- */
> db.test.insert({ a: 5, b: 10, c: 20, d: 12 });
> db.runCommand({ 'aggregate':'test', 'pipeline': [ { $project: { 'calculation': { $divide: [ $add: ['$a', '$b'], $add: ['$c', '$d'] ] } } } ] } );
Mon Mar 12 14:12:16 SyntaxError: missing ] after element list (shell):1
> db.runCommand({ 'aggregate':'test', 'pipeline': [ { $project: { 'calculation': { $divide: [ { $add: ['$a', '$b'] }, { $add: ['$c', '$d'] } ] } } } ] } );
{
"result" : [
{
"_id" : ObjectId("4f5e620fa40e4f69fffa10b4"),
"calculation" : 0.46875
}
],
"ok" : 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment