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

$in (operador de predicado de query)

$in

The $in operator selects the documents where the value of a field equals any value in the specified array.

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

  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud
  • 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

The $in operator has the following form:

{ field: { $in: [ <value1>, <value2>, ... <valueN> ] } }

If field has an array, the $in operator selects the documents whose field has an array that contains at least one element that matches a value in the specified array. For example, <value1>, <value2>, and so on.

$in compares each parameter to each document in the collection, which can cause performance issues. To improve performance, create an index on the field you want to query. An index allows MongoDB to create bounds for each $in element and search more efficiently.

Observação

This document describes the $in query operator. For the $in aggregation operator, see $in (expression operator).

For data stored in MongoDB Atlas, you can use the Atlas Search in Operator operator when running $search queries. Running $in after $search is less performant than running $search with the in Operator operator.

To learn more about the Atlas Search version of this operator, see the in Operator operator in the Atlas documentation.

Para criar a coleção inventory usada nos exemplos, execute:

db.inventory.insertMany( [
{ item: "Pens", quantity: 350, tags: [ "school", "office" ] },
{ item: "Erasers", quantity: 15, tags: [ "school", "home" ] },
{ item: "Maps", tags: [ "office", "storage" ] },
{ item: "Books", quantity: 5, tags: [ "school", "storage", "home" ] }
] )

Esta query seleciona documentos na coleção inventory em que o valor do campo quantity é 5 ou 15:

db.inventory.find( { quantity: { $in: [ 5, 15 ] } }, { _id: 0 } )

A saída:

{ item: 'Erasers', quantity: 15, tags: [ 'school', 'home' ] },
{ item: 'Books', quantity: 5, tags: [ 'school', 'storage', 'home' ] }

Although you can write the query using the $or operator, use the $in operator rather than the $or operator when performing equality checks on the same field.

A operação updateMany() a seguir define o campo exclude como false quando a array tags tiver pelo menos um elemento que corresponda a "home" ou "school":

db.inventory.updateMany(
{ tags: { $in: [ "home", "school" ] } },
{ $set: { exclude: false } }
)

Saída de exemplo:

{
item: 'Pens',
quantity: 350,
tags: [ 'school', 'office' ],
exclude: false
},
{
item: 'Erasers',
quantity: 15,
tags: [ 'school', 'home' ],
exclude: false
},
{
item: 'Books',
quantity: 5,
tags: [ 'school', 'storage', 'home' ],
exclude: false
}

Para obter exemplos adicionais sobre como consultar arrays, consulte:

Para obter exemplos adicionais sobre queries, consulte Ler documentos.

The $in operator can select documents using regular expressions of the form /pattern/. You cannot use $regex expressions inside $in.

Esta query seleciona documentos na coleção inventory em que o campo tags começa com be ou st:

db.inventory.find( { tags: { $in: [ /^be/, /^st/ ] } } )

A query seleciona todos os documentos na coleção inventory em que o campo tags contém uma string que começa com be ou st ou uma array com pelo menos um elemento que começa com be ou st.