Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 13th, 2012  |  syntax: None  |  size: 3.70 KB  |  hits: 9  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /* sample aggregate command queries */
  2. // make sure we're using the right db; this is the same as "use mydb;" in shell
  3. db = db.getSisterDB("aggdb");
  4.  
  5. // just passing through fields
  6. var p1 = db.runCommand(
  7. { aggregate : "article", pipeline : [
  8.     { $project : {
  9.         tags : 1,
  10.         pageViews : 1
  11.     }}
  12. ]});
  13.  
  14. // unwinding an array
  15. var p2 = db.runCommand(
  16. { aggregate : "article", pipeline : [
  17.     { $project : {
  18.         author : 1,
  19.         tag : { $unwind : "$tags" },
  20.         pageViews : 1
  21.     }}
  22. ]});
  23.  
  24. // pulling values out of subdocuments
  25. var p3 = db.runCommand(
  26. { aggregate : "article", pipeline : [
  27.     { $project : {
  28.         otherfoo : "$other.foo",
  29.         otherbar : "$other.bar"
  30.     }}
  31. ]});
  32.  
  33. // projection includes a computed value
  34. var p4 = db.runCommand(
  35. { aggregate : "article", pipeline : [
  36.     { $project : {
  37.         author : 1,
  38.         daveWroteIt : { $eq:["$author", "dave"] }
  39.     }}
  40. ]});
  41.  
  42. // projection includes a virtual (fabricated) document
  43. var p5 = db.runCommand(
  44. { aggregate : "article", pipeline : [
  45.     { $project : {
  46.         author : 1,
  47.         pageViews : 1,
  48.         tag : { $unwind : "$tags" }
  49.     }},
  50.     { $project : {
  51.         author : 1,
  52.         subDocument : { foo : "$pageViews", bar : "$tag"  }
  53.     }}
  54. ]});
  55.  
  56. // multi-step aggregate
  57. // nested expressions in computed fields
  58. var p6 = db.runCommand(
  59. { aggregate : "article", pipeline : [
  60.     { $project : {
  61.         author : 1,
  62.         tag : { $unwind : "$tags" },
  63.         pageViews : 1
  64.     }},
  65.     { $project : {
  66.         author : 1,
  67.         tag : 1,
  68.         pageViews : 1,
  69.         daveWroteIt : { $eq:["$author", "dave"] },
  70.         weLikeIt : { $or:[ { $eq:["$author", "dave"] },
  71.                            { $eq:["$tag", "good"] } ] }
  72.     }}
  73. ]});
  74.  
  75. // slightly more complex computed expression; $ifNull
  76. var p7 = db.runCommand(
  77. { aggregate : "article", pipeline : [
  78.     { $project : {
  79.         theSum : { $add:["$pageViews",
  80.                          { $ifNull:["$other.foo",
  81.                                     "$other.bar"] } ] }
  82.     }}
  83. ]});
  84.  
  85. // dotted path inclusion; _id exclusion
  86. var p8 = db.runCommand(
  87. { aggregate : "article", pipeline : [
  88.     { $project : {
  89.         _id : 0,
  90.         author : 1,
  91.         tag : { $unwind : "$tags" },
  92.         "comments.author" : 1
  93.     }}
  94. ]});
  95.  
  96.  
  97. // simple matching
  98. var m1 = db.runCommand(
  99. { aggregate : "article", pipeline : [
  100.     { $match : { author : "dave" } }
  101. ]});
  102.  
  103. // combining matching with a projection
  104. var m2 = db.runCommand(
  105. { aggregate : "article", pipeline : [
  106.     { $project : {
  107.         title : 1,
  108.         author : 1,
  109.         pageViews : 1,
  110.         tag : { $unwind : "$tags" },
  111.         comments : 1
  112.     }},
  113.     { $match : { tag : "nasty" } }
  114. ]});
  115.  
  116.  
  117. // group by tag
  118. var g1 = db.runCommand(
  119. { aggregate : "article", pipeline : [
  120.     { $project : {
  121.         author : 1,
  122.         tag : { $unwind : "$tags" },
  123.         pageViews : 1
  124.     }},
  125.     { $group : {
  126.         _id: { tag : 1 },
  127.         docsByTag : { $sum : 1 },
  128.         viewsByTag : { $sum : "$pageViews" }
  129.     }}
  130. ]});
  131.  
  132. // $max, and averaging in a final projection
  133. var g2 = db.runCommand(
  134. { aggregate : "article", pipeline : [
  135.     { $project : {
  136.         author : 1,
  137.         tag : { $unwind : "$tags" },
  138.         pageViews : 1
  139.     }},
  140.     { $group : {
  141.         _id: { tag : 1 },
  142.         docsByTag : { $sum : 1 },
  143.         viewsByTag : { $sum : "$pageViews" },
  144.         mostViewsByTag : { $max : "$pageViews" },
  145.     }},
  146.     { $project : {
  147.         _id: false,
  148.         tag : "$_id.tag",
  149.         mostViewsByTag : 1,
  150.         docsByTag : 1,
  151.         viewsByTag : 1,
  152.         avgByTag : { $divide:["$viewsByTag", "$docsByTag"] }
  153.     }}
  154. ]});
  155.  
  156. // $addToSet as an accumulator; can pivot data
  157. var g3 = db.runCommand(
  158. { aggregate : "article", pipeline : [
  159.     { $project : {
  160.         author : 1,
  161.         tag : { $unwind : "$tags" }
  162.     }},
  163.     { $group : {
  164.         _id : { tag : 1 },
  165.         authors : { $addToSet : "$author" }
  166.     }}
  167. ]});
  168.  
  169. // $avg, and averaging in a final projection
  170. var g4 = db.runCommand(
  171. { aggregate : "article", pipeline : [
  172.     { $project : {
  173.         author : 1,
  174.         tag : { $unwind : "$tags" },
  175.         pageViews : 1
  176.     }},
  177.     { $group : {
  178.         _id: { tag : 1 },
  179.         docsByTag : { $sum : 1 },
  180.         viewsByTag : { $sum : "$pageViews" },
  181.         avgByTag : { $avg : "$pageViews" },
  182.     }}
  183. ]});