Learn the "why" behind slow queries and how to fix them in our 2-Part Webinar.
Register now >
Menu Docs
Página inicial do Docs
/ /

$expr (operador de predicado da query)

Alterado na versão 5.0.

$expr

Permite o uso de expressões dentro de um predicado de query.

Você pode utilizar o $expr 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

{ $expr: { <expression> } }

O argumento pode ser qualquer expressão válida.

Quando $expr aparece em um estágio $match que faz parte de um subpipeline $lookup, $expr pode se referir a variáveis let definidas pelo estágio $lookup. Para obter um exemplo, consulte Usar várias condições de junção e uma subquery correlacionada.

Os operadores de comparação $eq, $lt, $lte, $gt e $gte colocados em um operador $expr podem utilizar um índice na coleção from referenciada em um estágio $lookup. Limitações:

  • Os índices só podem ser usados para comparações entre campos e constantes, portanto, o operando let deve ser resolvido para uma constante.

    Por exemplo, uma comparação entre $a e um valor constante pode usar um índice, mas uma comparação entre $a e $b não pode.

  • Os índices não são usados para comparações onde o operando let resolve para um valor vazio ou ausente.

  • Índices multichave, parciais ou esparsos não são usados.

Os exemplos nesta página usam dados do conjunto de dados de amostra sample_mflix. Para obter detalhes sobre como carregar esse conjunto de dados em sua implantação autogerenciada do MongoDB , consulte Carregar o conjunto de dados de amostra. Se você fez modificações nos bancos de dados de amostra, talvez seja necessário descartar e recriar os bancos de dados para executar os exemplos nesta página.

$expr pode conter expressões que comparam campos do mesmo documento.

The following operation uses $expr to find documents in the movies collection where the Rotten Tomatoes viewer rating exceeds the critic rating:

db.movies.find(
{ $expr: { $gt: [ "$tomatoes.viewer.rating", "$tomatoes.critic.rating" ] } },
{ _id: 0, title: 1, "tomatoes.viewer.rating": 1, "tomatoes.critic.rating": 1 }
).sort( { "tomatoes.viewer.rating": -1 } ).limit( 3 )
[
{ title: 'I Am Maria', tomatoes: { viewer: { rating: 5 } } },
{ title: 'Kadin Hamlet', tomatoes: { viewer: { rating: 5 } } },
{ title: 'The Seine Meets Paris', tomatoes: { viewer: { rating: 5 } } }
]

Some queries need to execute conditional logic when defining a query filter. The aggregation pipeline provides the $cond operator to express conditional statements. By using $expr with the $cond operator, you can specify a conditional filter for your query statement.

Assume you want to calculate a weighted score for movies so that highly-rated movies with few votes do not dominate the results:

  • If imdb.votes is greater than or equal to 1000, the weighted score is the full imdb.rating.

  • If imdb.votes is less than 1000, the weighted score is 0.5 of the imdb.rating.

You would like to know which movies in the movies collection have a weighted score greater than 9.

The following example uses $expr with $cond to calculate the weighted score based on imdb.votes and $gt to return documents whose calculated weighted score is greater than 9:

db.movies.find(
{
"imdb.rating": { $type: "number" },
"imdb.votes": { $type: "number" },
$expr: {
$gt: [
{
$cond: {
if: { $gte: ["$imdb.votes", 1000] },
then: { $multiply: ["$imdb.rating", 1.0] },
else: { $multiply: ["$imdb.rating", 0.5] }
}
},
9
]
}
},
{ _id: 0, title: 1, "imdb.rating": 1, "imdb.votes": 1 }
).sort( { title: 1 } ).limit(5)

The following table shows the weighted score for selected documents and whether the weighted score is greater than 9 (i.e. whether the document meets the query condition).

Documento
Weighted Score
> 9

{ title: "The Shawshank Redemption", imdb: { rating: 9.3, votes: 1521105 } }

9.3

true

{ title: "The Godfather", imdb: { rating: 9.2, votes: 1038358 } }

9.2

true

{ title: "Fight Club", imdb: { rating: 8.9, votes: 1191784 } }

8.9

false

{ title: "Planet Earth", imdb: { rating: 9.5, votes: 82896 } }

9.5

true

{ title: "Hollywood", imdb: { rating: 9.1, votes: 511 } }

4.55

false

The db.collection.find() operation returns 5 documents whose calculated weighted score is greater than 9:

[
{ title: 'Band of Brothers', imdb: { rating: 9.6, votes: 183802 } },
{ title: 'Baseball', imdb: { rating: 9.1, votes: 2460 } },
{ title: 'Cosmos', imdb: { rating: 9.3, votes: 17174 } },
{ title: 'Frozen Planet', imdb: { rating: 9.2, votes: 5903 } },
{ title: 'Human Planet', imdb: { rating: 9.2, votes: 9057 } }
]

Even though $cond calculates a weighted score, that score is not reflected in the returned documents. Instead, the returned documents represent the matching documents in their original state.

Voltar

Diversos

Nesta página