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

189
Visualizações
mongoose aggregate using $exists in $cond

I want to $project if a field exists, but not it's value, using mongoose model aggregate query. If it was possible using $exists in $cond, it would have looked something like this:

$project: {
    b: {
        $cond: {
            if    : {$exists: ['$b', true]},
            then  : true,
            else  : false
        }
    }
}

But, I have to use a boolean expression in the $cond operator. In the MongoDB shell, I can do something similar with:

{$eq: ['$b', undefined]}

and it yields the expected results, but with mongoose model aggregate for some reason, it always results with true.

for example, if I have the following documents:

{
    "a" : 1,
    "b" : 2
},
{
    "a" : 1
}

I need the following results:

{
    "b": true
},
{
    "b": false
}

How can I do something like that with mongoose?

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

0

$exists not supported in aggregate query of MongoDB. So in aggregate query instead of $exists can use $ifNull.

syntax:

{ $ifNull: [ <expression>, <replacement-expression-if-null> ] }

for more

Updated:

to get b value as true or false can try this query

db.test.aggregate([
    {
        $project: {
            b: { 
                $cond: [
                    {$ifNull: ['$b', false]}, // if
                    true, // then
                    false // else
                ]
            }
        }
    }
])

Explanation:

b = $cond: [ 'if condition satisfied', 'then true', 'else false' ];

where condition = {$ifNull: ['$b', false]} Here if $b not exist then condition = false otherwise condition = true.

so if condition = true then return then result that means b = true
if condition = false then return else result means b = false

over 4 years ago · Santiago Trujillo Relatório

0

You could use two $project statement for this case and make use of the $ifNull operator (Just wont work when some_field is set as false in which case you can change the inner false to something more suitable)

[
{
        $project: {
        test: {
            $ifNull: [
                '$some_field',
                false
            ]
        }
    }
},
{
    $project: {
        test: {
            $cond: {
                if    : {$eq: ['$test', false]},
                then  : false,
                else  : true
            }
        }
    }
}

])
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