Para agentes de IA: um índice de documentação está disponível em https://www.mongodb.com/pt-br/docs/llms.txt — as versões de markdown de todas as páginas estão disponíveis anexando .md a qualquer caminho de URL.
Menu Docs

$bucket (estágio de agregação )

$bucket

Categorizes incoming documents into groups, called buckets, based on a specified expression and bucket boundaries and outputs a document per each bucket. Each output document contains an _id field whose value specifies the inclusive lower bound of the bucket. The output option specifies the fields included in each output document.

$bucket produz somente documentos de saída para buckets que contenham pelo menos um documento de entrada.

The $bucket stage has a limit of 100 megabytes of RAM. By default, if the stage exceeds this limit, $bucket returns an error. To allow more space for stage processing, use the allowDiskUse option to enable aggregation pipeline stages to write data to temporary files.

{
$bucket: {
groupBy: <expression>,
boundaries: [ <lowerbound1>, <lowerbound2>, ... ],
default: <literal>,
output: {
<output1>: { <$accumulator expression> },
...
<outputN>: { <$accumulator expression> }
}
}
}

The $bucket document contains the following fields:

Campo
Tipo
Descrição

expressão

Uma expressão pela qual agrupar documentos. Para especificar um caminho do campo, prefixe o nome do campo com um sinal de dólar $ e coloque-o entre aspas.

Unless $bucket includes a default specification, each input document must resolve the groupBy field path or expression to a value that falls within one of the ranges specified by the boundaries.

array

Uma array de valores com base na expressão groupBy que especifica os limites de cada bucket. Cada par adjacente de valores atua como o limite inferior inclusivo e o limite superior exclusivo para o bucket. Você deve especificar pelo menos dois limites.

Os valores especificados devem estar em ordem crescente e todos do mesmo tipo. Exceto quando os valores são tipos numéricos mistos, como:

[ 10, Long(20), Int32(30) ]

Por exemplo, uma array de [ 0, 5, 10 ] cria dois buckets:

  • [0, 5) com limite inferior inclusivo 0 e limite superior exclusivo 5.

  • [5, 10) com limite inferior inclusivo 5 e limite superior exclusivo 10.

literal

Opcional. Um literal que especifica o _id de um bucket adicional que contém todos os documentos cujo resultado da expressão groupBy não se enquadra em um bucket especificado por limites.

Se não for especificado, cada documento de entrada deverá resolver a expressão groupBy para um valor dentro de um dos intervalos de bucket especificados por boundaries ou a operação gerará um erro.

O valor default deve ser menor que o valor boundaries mais baixo ou maior ou igual ao valor boundaries mais alto.

O valor default pode ser de um tipo diferente das entradas em boundaries.

documento

Opcional. Um documento que especifica os campos a serem incluídos nos documentos de saída, além do campo _id. Para especificar o campo a incluir, você deve usar acumulador de expressões.

<outputfield1>: { <accumulator>: <expression1> },
...
<outputfieldN>: { <accumulator>: <expressionN> }

Se você não especificar um documento de output, a operação retornará um campo count que contém o número de documentos em cada bucket.

Se você especificar um documento de output, somente os campos especificados no documento serão retornados; ou seja, o campo count não será retornado a menos que seja explicitamente incluído no documento de output.

$bucket requer que pelo menos uma das seguintes condições seja atendida ou que a operação apresente um erro:

  • Each input document resolves the groupBy expression to a value within one of the bucket ranges specified by boundaries, or

  • A default value is specified to bucket documents whose groupBy values are outside of the boundaries or of a different BSON type than the values in boundaries.

Se a expressão groupBy for resolvida para uma array ou um documento, $bucket organizará os documentos de entrada em buckets utilizando a lógica de comparação de $sort.

In mongosh, create a sample collection named artists with the following documents:

db.artists.insertMany([
{ "_id" : 1, "last_name" : "Bernard", "first_name" : "Emil", "year_born" : 1868, "year_died" : 1941, "nationality" : "France" },
{ "_id" : 2, "last_name" : "Rippl-Ronai", "first_name" : "Joszef", "year_born" : 1861, "year_died" : 1927, "nationality" : "Hungary" },
{ "_id" : 3, "last_name" : "Ostroumova", "first_name" : "Anna", "year_born" : 1871, "year_died" : 1955, "nationality" : "Russia" },
{ "_id" : 4, "last_name" : "Van Gogh", "first_name" : "Vincent", "year_born" : 1853, "year_died" : 1890, "nationality" : "Holland" },
{ "_id" : 5, "last_name" : "Maurer", "first_name" : "Alfred", "year_born" : 1868, "year_died" : 1932, "nationality" : "USA" },
{ "_id" : 6, "last_name" : "Munch", "first_name" : "Edvard", "year_born" : 1863, "year_died" : 1944, "nationality" : "Norway" },
{ "_id" : 7, "last_name" : "Redon", "first_name" : "Odilon", "year_born" : 1840, "year_died" : 1916, "nationality" : "France" },
{ "_id" : 8, "last_name" : "Diriks", "first_name" : "Edvard", "year_born" : 1855, "year_died" : 1930, "nationality" : "Norway" }
])

A operação a seguir agrupa os documentos em buckets de acordo com o campo year_born e filtra com base na contagem de documentos nos buckets:

db.artists.aggregate( [
// First Stage
{
$bucket: {
groupBy: "$year_born", // Field to group by
boundaries: [ 1840, 1850, 1860, 1870, 1880 ], // Boundaries for the buckets
default: "Other", // Bucket ID for documents which do not fall into a bucket
output: { // Output for each bucket
"count": { $sum: 1 },
"artists" :
{
$push: {
"name": { $concat: [ "$first_name", " ", "$last_name"] },
"year_born": "$year_born"
}
}
}
}
},
// Second Stage
{
$match: { count: {$gt: 3} }
}
] )
Primeira etapa

The $bucket stage groups the documents into buckets by the year_born field. The buckets have the following boundaries:

  • [1840, 1850) com limite inferior inclusivo de 1840 e limite superior exclusivo de 1850.

  • [1850, 1860) com limite inferior inclusivo 1850 e limite superior exclusivo 1860.

  • [1860, 1870) com limite inferior inclusivo 1860 e limite superior exclusivo 1870.

  • [1870, 1880) com limite inferior inclusivo 1870 e limite superior exclusivo 1880.

  • If a document did not contain the year_born field or its year_born field was outside the ranges above, it would be placed in the default bucket with the _id value "Other".

The stage includes the output document to determine the fields to return:

Campo
Descrição

_id

Limite inferior inclusivo do bucket.

count

Contagem de documentos no bucket.

artists

Array de documentos contendo informações sobre cada artista no bucket. Cada documento contém o seguinte sobre o artista:

  • name, que é uma concatenação (ou seja, $concat) do first_name e last_name do artista.

  • year_born

Este estágio passa os seguintes documentos para o próximo estágio:

{ "_id" : 1840, "count" : 1, "artists" : [ { "name" : "Odilon Redon", "year_born" : 1840 } ] }
{ "_id" : 1850, "count" : 2, "artists" : [ { "name" : "Vincent Van Gogh", "year_born" : 1853 },
{ "name" : "Edvard Diriks", "year_born" : 1855 } ] }
{ "_id" : 1860, "count" : 4, "artists" : [ { "name" : "Emil Bernard", "year_born" : 1868 },
{ "name" : "Joszef Rippl-Ronai", "year_born" : 1861 },
{ "name" : "Alfred Maurer", "year_born" : 1868 },
{ "name" : "Edvard Munch", "year_born" : 1863 } ] }
{ "_id" : 1870, "count" : 1, "artists" : [ { "name" : "Anna Ostroumova", "year_born" : 1871 } ] }
Segunda etapa

O estágio $match filtra a saída do estágio anterior para retornar apenas buckets que contenham mais de três documentos.

A operação retorna o seguinte documento:

{ "_id" : 1860, "count" : 4, "artists" :
[
{ "name" : "Emil Bernard", "year_born" : 1868 },
{ "name" : "Joszef Rippl-Ronai", "year_born" : 1861 },
{ "name" : "Alfred Maurer", "year_born" : 1868 },
{ "name" : "Edvard Munch", "year_born" : 1863 }
]
}

You can use the $facet stage to perform multiple $bucket aggregations in a single stage.

In mongosh, create a sample collection named artwork with the following documents:

db.artwork.insertMany([
{ "_id" : 1, "title" : "The Pillars of Society", "artist" : "Grosz", "year" : 1926,
"price" : Decimal128("199.99") },
{ "_id" : 2, "title" : "Melancholy III", "artist" : "Munch", "year" : 1902,
"price" : Decimal128("280.00") },
{ "_id" : 3, "title" : "Dancer", "artist" : "Miro", "year" : 1925,
"price" : Decimal128("76.04") },
{ "_id" : 4, "title" : "The Great Wave off Kanagawa", "artist" : "Hokusai",
"price" : Decimal128("167.30") },
{ "_id" : 5, "title" : "The Persistence of Memory", "artist" : "Dali", "year" : 1931,
"price" : Decimal128("483.00") },
{ "_id" : 6, "title" : "Composition VII", "artist" : "Kandinsky", "year" : 1913,
"price" : Decimal128("385.00") },
{ "_id" : 7, "title" : "The Scream", "artist" : "Munch", "year" : 1893
/* No price*/ },
{ "_id" : 8, "title" : "Blue Flower", "artist" : "O'Keefe", "year" : 1918,
"price" : Decimal128("118.42") }
])

The following operation uses two $bucket stages within a $facet stage to create two groupings, one by price and the other by year:

db.artwork.aggregate( [
{
$facet: { // Top-level $facet stage
"price": [ // Output field 1
{
$bucket: {
groupBy: "$price", // Field to group by
boundaries: [ 0, 200, 400 ], // Boundaries for the buckets
default: "Other", // Bucket ID for documents which do not fall into a bucket
output: { // Output for each bucket
"count": { $sum: 1 },
"artwork" : { $push: { "title": "$title", "price": "$price" } },
"averagePrice": { $avg: "$price" }
}
}
}
],
"year": [ // Output field 2
{
$bucket: {
groupBy: "$year", // Field to group by
boundaries: [ 1890, 1910, 1920, 1940 ], // Boundaries for the buckets
default: "Unknown", // Bucket ID for documents which do not fall into a bucket
output: { // Output for each bucket
"count": { $sum: 1 },
"artwork": { $push: { "title": "$title", "year": "$year" } }
}
}
}
]
}
}
] )
Primeira faceta

A primeira faceta agrupa os documentos de entrada por price. Os buckets têm os seguintes limites:

  • [0, 200) com limite inferior inclusivo 0 e limite superior exclusivo 200.

  • [200, 400) com limite inferior inclusivo 200 e limite superior exclusivo 400.

  • "Other", o bucket default que contém documentos sem preços ou preços fora das faixas acima.

The $bucket stage includes the output document to determine the fields to return:

Campo
Descrição

_id

Limite inferior inclusivo do bucket.

count

Contagem de documentos no bucket.

artwork

Array de documentos contendo informações sobre cada obra de arte no bucket.

averagePrice

Emprega o operador $avg para exibir o preço médio de todas as obras de arte no bucket.

Segunda faceta

A segunda faceta agrupa os documentos de entrada por year. Os buckets têm os seguintes limites:

  • [1890, 1910) com limite inferior inclusivo de 1890 e limite superior exclusivo de 1910.

  • [1910, 1920) com limite inferior inclusivo de 1910 e limite superior exclusivo de 1920.

  • [1920, 1940) com limite inferior inclusivo de 1910 e limite superior exclusivo de 1940.

  • " Unknown ", o bucket default contendo documentos sem anos ou anos fora dos intervalos acima.

The $bucket stage includes the output document to determine the fields to return:

Campo
Descrição

count

Contagem de documentos no bucket.

artwork

Array de documentos contendo informações sobre cada obra de arte no bucket.

Saída

A operação retorna o seguinte documento:

{
"price" : [ // Output of first facet
{
"_id" : 0,
"count" : 4,
"artwork" : [
{ "title" : "The Pillars of Society", "price" : Decimal128("199.99") },
{ "title" : "Dancer", "price" : Decimal128("76.04") },
{ "title" : "The Great Wave off Kanagawa", "price" : Decimal128("167.30") },
{ "title" : "Blue Flower", "price" : Decimal128("118.42") }
],
"averagePrice" : Decimal128("140.4375")
},
{
"_id" : 200,
"count" : 2,
"artwork" : [
{ "title" : "Melancholy III", "price" : Decimal128("280.00") },
{ "title" : "Composition VII", "price" : Decimal128("385.00") }
],
"averagePrice" : Decimal128("332.50")
},
{
// Includes documents without prices and prices greater than 400
"_id" : "Other",
"count" : 2,
"artwork" : [
{ "title" : "The Persistence of Memory", "price" : Decimal128("483.00") },
{ "title" : "The Scream" }
],
"averagePrice" : Decimal128("483.00")
}
],
"year" : [ // Output of second facet
{
"_id" : 1890,
"count" : 2,
"artwork" : [
{ "title" : "Melancholy III", "year" : 1902 },
{ "title" : "The Scream", "year" : 1893 }
]
},
{
"_id" : 1910,
"count" : 2,
"artwork" : [
{ "title" : "Composition VII", "year" : 1913 },
{ "title" : "Blue Flower", "year" : 1918 }
]
},
{
"_id" : 1920,
"count" : 3,
"artwork" : [
{ "title" : "The Pillars of Society", "year" : 1926 },
{ "title" : "Dancer", "year" : 1925 },
{ "title" : "The Persistence of Memory", "year" : 1931 }
]
},
{
// Includes documents without a year
"_id" : "Unknown",
"count" : 1,
"artwork" : [
{ "title" : "The Great Wave off Kanagawa" }
]
}
]
}

Para saber mais sobre os estágios de pipeline relacionados, consulte o guia$bucketAuto.