Uploaded image for project: 'Drivers'
  1. Drivers
  2. DRIVERS-448

Aggregation, runCommand, and index management examples for Docs

    XMLWordPrintableJSON

Details

    • $i18n.getText("admin.common.words.hide")
      Key Status/Resolution FixVersion
      NODE-1323 Fixed 3.1.12
      PYTHON-1465 Fixed 3.7
      CSHARP-2183 Fixed
      PERL-859 Fixed 2.0.0
      RUBY-1296 Fixed
      JAVA-2781 Fixed
      CXX-1514 Fixed 3.3.0-rc0
      PHPLIB-326 Fixed 1.3.2
      MOTOR-195 Fixed 2.0
      GODRIVER-225 Fixed
      CDRIVER-2500 Fixed 1.11.0
      SWIFT-982 Won't Do
      RUST-537 Fixed 2.1.0
      $i18n.getText("admin.common.words.show")
      #scriptField, #scriptField *{ border: 1px solid black; } #scriptField{ border-collapse: collapse; } #scriptField td { text-align: center; /* Center-align text in table cells */ } #scriptField td.key { text-align: left; /* Left-align text in the Key column */ } #scriptField a { text-decoration: none; /* Remove underlines from links */ border: none; /* Remove border from links */ } /* Add green background color to cells with FixVersion */ #scriptField td.hasFixVersion { background-color: #00FF00; /* Green color code */ } /* Center-align the first row headers */ #scriptField th { text-align: center; } Key Status/Resolution FixVersion NODE-1323 Fixed 3.1.12 PYTHON-1465 Fixed 3.7 CSHARP-2183 Fixed PERL-859 Fixed 2.0.0 RUBY-1296 Fixed JAVA-2781 Fixed CXX-1514 Fixed 3.3.0-rc0 PHPLIB-326 Fixed 1.3.2 MOTOR-195 Fixed 2.0 GODRIVER-225 Fixed CDRIVER-2500 Fixed 1.11.0 SWIFT-982 Won't Do RUST-537 Fixed 2.1.0

    Description

      Here are the examples. They should be delimited with

      // Start <delimiter>
      [code]
      // End <delimiter>
      

      as has been done for the CRUD examples and such. I've attached the mongoexported datasets I used for the agg examples if one wants to make sure everything is working.

      Delimiter Shell Snippet Notes
      Aggregation Example 1

      db.sales.aggregate(
        [
          { $match : { "items.fruit":"banana" } },
          { $sort : { "date" : 1 } }
      ])
      

      Simple agg example
      Aggregation Example 2

      db.sales.aggregate(
         [
            {
               $unwind: "$items"
            },
            { $match: {
               "items.fruit" : "banana",
            }},
            {
              $group : {
                 _id : { day: { $dayOfWeek: "$date" } },
                 count: { $sum: "$items.quantity" }
              }
            },
            {
              $project: {
                dayOfWeek: "$_id.day",
                numberSold: "$count",
                _id:0
              }
            },
            {
              $sort: { "numberSold": 1 }
            }
         ]
      )
      

      Aggregation - $match, $group, $project, $unwind, $sum, $sort, $dayOfWeek
      Aggregation Example 3

      db.sales.aggregate([
        {
           $unwind: "$items"
        },
        {
          $group : {
             _id : { day : { $dayOfWeek : "$date" } },
             items_sold : { $sum : "$items.quantity" },
             revenue: { $sum : { $multiply : [ "$items.quantity", "$items.price" ] } }
          }
        },
        {
          $project : {
            day : "$_id.day",
            revenue : 1,
            items_sold : 1,
            discount: {
              $cond : { if : { $lte : [ "$revenue", 250 ] }, then : 25, else : 0 }
            }
          }
        }
       ])
      

      Aggregation - $unwind, $group, $sum, $dayOfWeek, $multiply, $project, $cond.
      If you're testing this one, change $revenue, 250 to a smaller number since the dataset I've attached doesn't have that many documents currently
      Aggregation Example 4

      db.air_alliances.aggregate( [
         {
            $lookup : {
              from : "air_airlines",
              let : { constituents: "$airlines" },
              pipeline : [
                {
                  $match : { $expr : { $in : [ "$name", "$$constituents" ] } }
                  }
              ],
              as : "airlines"
            }
         },
         {
           $project : {
             "_id" : 0,
             "name" : 1,
             airlines : {
                $filter : {
                  input : "$airlines",
                  as : "airline",
                  cond : { $eq: ["$$airline.country", "Canada"] }
                }
             }
           }
         }
      ])
      

      Aggregation - $lookup, $filter, $match
      runCommand Example 1

      db.runCommand({buildInfo: 1})

       
      runCommand Example 2

      db.runCommand({collStats:"restaurants"})

      No longer applicable. See DRIVERS-2232.
      Index Example 1

      db.records.createIndex( { score: 1 } )
      

      Indexes - builds simple ascending index
      Index Example 2

      db.restaurants.createIndex(
         { cuisine: 1, name: 1 },
         { partialFilterExpression: { rating: { $gt: 5 } } }
      )
      

      Indexes - builds multikey index with partial filter expression

      Attachments

        1. air_airlines.json
          1.02 MB
        2. air_alliances.json
          1 kB
        3. sales.json
          1.0 kB

        Activity

          People

            Unassigned Unassigned
            rathi.gnanasekaran Rathi Gnanasekaran
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: