ANNOUNCEMENT: Voyage AI joins MongoDB to power more accurate and trustworthy AI applications on Atlas.
Learn more
Menu Docs

$gte

$gte

$gte selects the documents where the value of the specified field is greater than or equal to (i.e. >=) a specified value (e.g. value.)

Para a maioria dos tipos de dados, operadores de comparação só realizam comparações nos campos em que o tipo de BSON corresponde ao tipo do valor da query. O MongoDB suporta comparação limitada entre BSONs por meio de bracketing de tipo.

Você pode utilizar o $gte para implantações hospedadas nos seguintes ambientes:

  • MongoDB Atlas: o serviço totalmente gerenciado para implantações do MongoDB na nuvem

  • MongoDB Enterprise: a versão autogerenciada e baseada em assinatura do MongoDB

  • MongoDB Community: uma versão com código disponível, de uso gratuito e autogerenciada do MongoDB

O operador $gte tem o seguinte formato:

{ field: { $gte: value } }

Os exemplos a seguir usam a collection inventory. Criar a collection:

db.inventory.insertMany( [
{
"item": "nuts", "quantity": 30,
"carrier": { "name": "Shipit", "fee": 3 }
},
{
"item": "bolts", "quantity": 50,
"carrier": { "name": "Shipit", "fee": 4 }
},
{
"item": "washers", "quantity": 10,
"carrier": { "name": "Shipit", "fee": 1 }
}
] )

Select all documents in the inventory collection where quantity is greater than or equal to 20:

db.inventory.find( { quantity: { $gte: 20 } } )

Saída de exemplo:

{
_id: ObjectId("61bb51211b83c864e3bbe037"),
item: 'nuts',
quantity: 30,
carrier: { name: 'Shipit', fee: 3 }
},
{
_id: ObjectId("61bb51211b83c864e3bbe038"),
item: 'bolts',
quantity: 50,
carrier: { name: 'Shipit', fee: 4 }
}

O exemplo a seguir define o campo price com base em uma comparação $gte com um campo em um documento incorporado.

db.inventory.updateMany(
{ "carrier.fee": { $gte: 2 } }, { $set: { "price": 9.99 } }
)

Saída de exemplo:

{
_id: ObjectId("61bb51211b83c864e3bbe037"),
item: 'nuts',
quantity: 30,
carrier: { name: 'Shipit', fee: 3 },
price: 9.99
},
{
_id: ObjectId("61bb51211b83c864e3bbe038"),
item: 'bolts',
quantity: 50,
carrier: { name: 'Shipit', fee: 4 },
price: 9.99
}

This updateMany() operation searches for an embedded document, carrier, with a subfield named fee. It sets { price: 9.99 } in each document where fee has a value greater than or equal to 2.

To set the value of the price field in only the first document where carrier.fee is greater than 2, use updateMany().

Veja também: