[DRIVERS-448] Aggregation, runCommand, and index management examples for Docs Created: 02/Feb/18  Updated: 01/Jun/23  Resolved: 22/Mar/23

Status: Closed
Project: Drivers
Component/s: Docs Examples
Fix Version/s: None

Type: Task Priority: Major - P3
Reporter: Rathi Gnanasekaran Assignee: Unassigned
Resolution: Done Votes: 0
Labels: documentation
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: File air_airlines.json     File air_alliances.json     File sales.json    
Issue Links:
Depends
depends on CDRIVER-2500 Aggregation, runCommand, and index ma... Closed
depends on CSHARP-2183 Aggregation, runCommand, and index ma... Closed
depends on DOCS-11264 Agg/Index/runCommand examples for Dri... Closed
depends on GODRIVER-225 Aggregation, runCommand, and index ma... Closed
depends on JAVA-2781 Aggregation, runCommand, and index ma... Closed
depends on MOTOR-195 Aggregation, runCommand, and index ma... Closed
depends on NODE-1323 Aggregation, runCommand, and index ma... Closed
depends on PHPLIB-326 Aggregation, runCommand, and index ma... Closed
depends on RUBY-1296 Aggregation, runCommand, and index ma... Closed
depends on RUST-537 Aggregation, runCommand, and index ma... Closed
depends on CXX-1514 Aggregation, runCommand, and index ma... Closed
depends on PYTHON-1465 Aggregation, runCommand, and index ma... Closed
is depended on by DOCS-11268 Docs: Tabbed Examples Agg/Index/Command Closed
Related
Driver Compliance:
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


 Comments   
Comment by Githook User [ 01/Jun/23 ]

Author:

{'name': 'Jérôme Tamarelle', 'email': 'jerome.tamarelle@mongodb.com', 'username': 'GromNaN'}

Message: DRIVERS-448: Remove documentation example that used collStats (#1091)

https://jira.mongodb.org/browse/DRIVERS-448
Branch: master
https://github.com/mongodb/mongo-php-library/commit/f6551edd60a368f019bb7cd1dda4fe78a5e7597b

Comment by Githook User [ 28/Apr/23 ]

Author:

{'name': 'Kevin Albertson', 'email': 'kevin.albertson@mongodb.com', 'username': 'kevinAlbs'}

Message: CDRIVER-4556 Remove use of `collStats` (#1249)

  • sync specification tests to commit 6632b7606e74c6e83ee2d80b7571f55485c7ff33
  • remove runCommand Example 2

The example removed from DRIVERS-448.

  • suggest $collStats in mongoc_collection_stats
  • move example in mongoc_collection_command_simple to example source
  • add missing declaration of `str`
  • use `ping` instead of `collStats` command
  • move Executing Commands code to example
  • replace `collStats` with `ping`
  • use `mongoc_client_command_simple`

To simplify the example.
There is no need to run the command with the mongoc_collection_t handle.

  • remove note about returning cursors

`mongoc_collection_command`, `mongoc_database_command` and `mongoc_client_command` return a cursor. They are documented as `superseded` by other helpers.

  • skip test using collStats on servers > 6.0
  • remove redundant captions

Co-authored-by: Ezra Chung <88335979+eramongodb@users.noreply.github.com>

---------

Co-authored-by: Ezra Chung <88335979+eramongodb@users.noreply.github.com>
Branch: master
https://github.com/mongodb/mongo-c-driver/commit/2d27a1113362f648f75e27b6115fc666e4f75f74

Comment by Githook User [ 28/Apr/23 ]

Author:

{'name': 'Kevin Albertson', 'email': 'kevin.albertson@mongodb.com', 'username': 'kevinAlbs'}

Message: CXX-2637 Remove use of `collStats` and `currentOp` (#960)

  • sync specification tests to commit 6632b7606e74c6e83ee2d80b7571f55485c7ff33
  • remove runCommand Example 2

The example removed from DRIVERS-448.

  • replace currentOp command with $currentOp aggregation stage
  • remove check for server version 3.5.8

C driver 1.21.0 requires server version 3.6 (minWireVersion=6)
C++ driver 3.7.x requirees C driver 1.22.1 or higher.
Branch: master
https://github.com/mongodb/mongo-cxx-driver/commit/52a1150d6fcb8211a6fe6652813441551da3a63e

Comment by Githook User [ 08/Mar/18 ]

Author:

{'email': 'jmikola@gmail.com', 'name': 'Jeremy Mikola', 'username': 'jmikola'}

Message: Update createIndexes example for PHP

This example was linking to the current PHP driver but showing syntax from the legacy driver. Updated to correctly link to the current PHP library API. Keeping "PHP Driver" language in place for consistency, despite the fact that this is handled by the library.

These examples may change later with DRIVERS-448, but this is a quick fix.
Branch: master
https://github.com/mongodb/docs/commit/9704287013d423c604585f44c7bbebc6c6c1d7d9

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