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

85
Visualizações
Mongodb Compare two fields with ratio

Rows whose field is twice that of the other field explain with example :

{id : 1 , price : 10 , spent : 5}
{id : 1 , price : 20 , spent : 30}
{id : 1 , price : 40 , spent : 90}
{id : 1 , price : 80 , spent : 200}

I want row that spend is equal or bigger twice price (spent >= 2*price)

result :

{id : 1 , price : 40 , spent : 90} ==> (spent >= 2price) [90>80]*

{id : 1 , price : 80 , spent : 200} ==> (spent >= 2price) [160>200]*

please refer suggestion except use $where

my mongo cluster have limitations of use $where

(MongoError: $where is not allowed in this atlas tier)

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

MongoDB documentation is very good with examples.
There is a place Reference that has all the operators.
If you haven't found it, maybe try the bellow, i think this is what you need.

Query

  • we can refer to the field with $fieldName
  • and gt/multiply operators are used
  • finally $expr is used in match when we use aggregate operators (instead of query operaros) like we did here

Playmongo

aggregate(
[{"$match": {"$expr": {"$gt": ["$spent", {"$multiply": [2, "$price"]}]}}}])
about 4 years ago · Juan Pablo Isaza 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