FireDAC.Phys.MongoDBWrapper.TMongoCollection.Aggregate

From RAD Studio API Documentation
Jump to: navigation, search

Delphi

function Aggregate(APipeline: TMongoPipeline; AFlags: TMongoQueryFlags = []): IMongoCursor; overload;
function Aggregate(AFlags: TMongoQueryFlags = []): TMongoPipeline; overload;

C++

_di_IMongoCursor __fastcall Aggregate(TMongoPipeline* APipeline, TMongoQueryFlags AFlags = TMongoQueryFlags() )/* overload */;
TMongoPipeline* __fastcall Aggregate(TMongoQueryFlags AFlags = TMongoQueryFlags() )/* overload */;

Properties

Type Visibility Source Unit Parent
function public
FireDAC.Phys.MongoDBWrapper.pas
FireDAC.Phys.MongoDBWrapper.hpp
FireDAC.Phys.MongoDBWrapper TMongoCollection

Description

Applies the specified pipeline to the documents of the collection and returns a cursor to access the results.

Aggregate supports two different coding styles.

You can optionally specify a set of query flags.

Example

The following example obtains an aggregation of documents where each document has a zip code as identifier (_id) and the number of Italian restaurants in that zip code (count), sorted by the count field.

MongDB shell:

db.restaurants.aggregate([
    { $match: { cuisine: "Italian" } },
    { $group: { _id: "$address.zipcode", count: { $sum: 1 } } },
    { $sort: { count: -1 } }
])

Delphi:

MyCursor := MyCollection.Aggregate
  .Match
    .Add('cuisine', 'Italian')
  .&End
  .Group
    .Add('_id', '$address.zipcode')
    .BeginObject('count')
      .Add('$sum', 1)
    .EndObject
  .&End
  .Sort('{ count: -1 }').&End;

C++:

_di_IMongoCursor MyCursor = MyCollection->Aggregate()
  ->Match()
    ->Add("cuisine", String("Italian"))
  ->End()
  ->Group()
    ->Add("_id", String("$address.zipcode"))
    ->BeginObject("count")
      ->Add("$sum", 1)
    ->EndObject()
  ->End()
  ->Sort("{ count: -1 }")->End()
  ->Open();

See Also