4.3 Collection method aggregate()

The collection method aggregate() calculates aggregate values for the data in a collection or a view. Its definition is:

db.collection.aggregate(pipeline, options)

where pipeline is a sequence of data aggregation operations or stages, and options are additional options that aggregate() passes to the aggregate command.

4.3.1. Group the documents by title, and show titles that appear more than once, sorted by quantity and title

> db.pages.aggregate( [
...    {
...      $group: {
...         _id: "$title",
...         count: { $sum: 1 }
...      }
...    },
...    { $match: { count: { $gt: 1 } } },
...    { $sort: {  count: 1, "_id": 1} }
... ] );
{ "_id" : "Avenida Central", "count" : 2 }
{ "_id" : "Casa da Ópera", "count" : 2 }
{ "_id" : "Chafariz da Praça Quinze de Novembro", "count" : 2 }
{ "_id" : "Chafariz da Praça São Salvador", "count" : 2 }
{ "_id" : "Chafariz das Saracuras", "count" : 2 }
{ "_id" : "Cidade Nova", "count" : 2 }
{ "_id" : "Copacabana", "count" : 2 }
{ "_id" : "Igreja de São Francisco de Paula", "count" : 2 }
{ "_id" : "Morro da Viúva", "count" : 2 }
{ "_id" : "Ouro Preto", "count" : 2 }
{ "_id" : "Santa Luzia", "count" : 2 }
{ "_id" : "São Roque", "count" : 2 }
{ "_id" : "Chafariz das Marrecas", "count" : 3 }
{ "_id" : "Quinta da Boa Vista", "count" : 3 }
{ "_id" : "Floresta da Tijuca", "count" : 4 }
{ "_id" : "Largo da Carioca", "count" : 4 }
{ "_id" : "Canal do Mangue", "count" : 5 }
{ "_id" : "Passeio Público", "count" : 5 }
{ "_id" : "Jardim Botânico do Rio de Janeiro", "count" : 7 }
>

4. Text Search