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

237
Visualizações
How to unset fields based on the fields' name with a "less than" condition?

I am trying to unset certain fields based on the name of the field (the key).

Say I have something like this:

{
    5: "cool",
    93: "cool",
    30: "cool",
    56: "cool"
}

How would I unset all fields with a value below, say, 40.

So the result should be:

{
    5: "cool",
    30: "cool"
}

I tried using the less than command on the whole field or using the positional operator but both of those failed.

collection.update_one(
        {"_id": id},
        {"$unset": {"blacklist.$": {"$lt": 40}}}
    )

I couldn't find anything online or on the docs so I am hoping to find some help here,

Thanks!

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

0

You can't really use $unset like this, we can still achieve this using pipelined updates - with slightly more complicated syntax.

Our approach will be to turn our root object to an array using $objectToArray, iterate over it and filter all numeric keys under a certain threshold. then finally convert back to an object and update our document, like so:

db.collection.update({},
[
  {
    $replaceRoot: {
      newRoot: {
        $arrayToObject: {
          $filter: {
            input: {
              $objectToArray: "$$ROOT"
            },
            cond: {
              $cond: [
                {
                  $regexMatch: {
                    input: "$$this.k",
                    regex: "^[0-9]+$"
                  }
                },
                {
                  $lt: [
                    {
                      $toInt: "$$this.k"
                    },
                    40
                  ]
                },
                true
              ]
            }
          }
        }
      }
    }
  }
])

Mongo Playground

over 4 years ago · Santiago Trujillo Relatório

0

Query

  • into [] the $$ROOT
  • filter the keys to be either "_id" or < 40
  • back to object and
  • replace root

*this removes all fields >=40 , you can change the last $lt if you don't need this

*toms answer works and its more general, if you have mixed keys that some are numbers and some are not use toms way (this is slightly simpler but supposes that all fields are numbers except the "_id")

Test code here

aggregate(
[{"$replaceRoot":
  {"newRoot":
   {"$arrayToObject":
    [{"$filter":
      {"input":{"$objectToArray":"$$ROOT"},
       "cond":
       {"$or":
        [{"$eq":["$$this.k", "_id"]},
         {"$lt":[{"$toInt":"$$this.k"}, 40]}]}}}]}}}])
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