Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

325
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda