[JAVA-2781] Aggregation, runCommand, and index management examples for Docs Created: 09/Feb/18  Updated: 28/Oct/23  Resolved: 22/Aug/18

Status: Closed
Project: Java Driver
Component/s: Documentation
Affects Version/s: None
Fix Version/s: None

Type: Task Priority: Major - P3
Reporter: Rathi Gnanasekaran Assignee: Jeffrey Yemin
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends
is depended on by DRIVERS-448 Aggregation, runCommand, and index ma... Closed
is depended on by JAVA-2861 Make Java driver spec compliant - FY2... Closed
is depended on by JAVA-2911 Java FY2019 Q3 Quick Wins Closed

 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"})

 
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


 Comments   
Comment by Jeffrey Yemin [ 22/Aug/18 ]

Note that the Aggregation Example 4 depends on a fix for JAVA-2936, which has been pushed to master but is not yet released. If we want to get that example in the docs prior to a driver release (which is currently unscheduled, we'll have to re-write that example to not use Aggregates.lookup (which will look weird unless we removed all use of the Aggregates class.)

Comment by Githook User [ 22/Aug/18 ]

Author:

{'name': 'Jeff Yemin', 'email': 'jeff.yemin@10gen.com', 'username': 'jyemin'}

Message: JAVA-2781: Add documentation sample code

Add documentation sample code for aggregate, runCommand, and createIndex
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/1747919b4ac1681f359f3f336a6bdf3720e727eb

Generated at Thu Feb 08 08:58:03 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.