Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

330
Visualizações
updating a document using the $max operator on arrays

i have the following document:

 {
    _id: 12,
    item: 'envelope',
    qty: ISODate("2021-12-05T00:00:00.000Z"),
    arrayField: [ 128, 190, 1 ]
  }

and i try to update it using this command

products> db.products.update({_id:12},{$max : { arrayField : [1,190,1879]} })

the output is as follows:

{
  acknowledged: true,
  insertedId: null,
  matchedCount: 1,
  modifiedCount: 0,
  upsertedCount: 0
}

I don't really understand how the comparison between the existing arrayField and the new one is being done. They are both Arrays, so there should be some kind of comparison on every element, but how exactly does it work?

From the documentation i read this:

With arrays, a less-than comparison or an ascending sort compares the smallest element of arrays, and a greater-than comparison or a descending sort compares the largest element of the arrays. As such, when comparing a field whose value is a single-element array (e.g. 1 ) with non-array fields (e.g. 2), the comparison is between 1 and 2. A comparison of an empty array (e.g. [ ]) treats the empty array as less than null or a missing field.

But i still don't understand exactly... Could someone provide an example in my case? Thanks in advance

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

MongoDB takes the min or max, to represent the array in comparisons, for the query operators.

Numbers

{"ar" : [1,2,3]}

(<= ar 1) => (<= min(1,2,3) 3) => (<= 1 3)  true
(>= ar 3) => (>= max(1,2,3) 3) => (<= 3 3) true

(= ar 2)  => true because it contains the element

For empty arrays either < or > its always false compared to a number

Arrays (again take the min if < , or max it >)

{"ar" : [1,2,3]}

(<= ar [0 1 2 3]) false because its like min(ar)=1 <min(0,1,2,3)=0

(= ar [1]) false we need all elements =

For the update $max operator

if both are arrays => elements are compared one by one.

max [1 2 3] [5] => [5]
max [1 2] [1 2 -100] => [1 2 -100]

Those are only for the $gte,$gt,$lt,$lte, $eq query operators.
The aggregate ones with same names are strict and don't work like this.

*Its not like complete because we have many types,i think the above are ok, answer might help, it was big to fit in comments.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda