Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 551
TomG Avatar asked Jan 09 '17 07:01

TomG


People also ask

Can we use count with aggregate function in MongoDB?

MongoDB $count AggregationThe MongoDB $count operator allows us to pass a document to the next phase of the aggregation pipeline that contains a count of the documents. There a couple of important things to note about this syntax: First, we invoke the $count operator and then specify the string.

Can we use $and in aggregate MongoDB?

You can use $and with aggregation but you don't have to write it, and is implicit using different filters, in fact you can pipe those filters in case one of them needs a different solution.

What are the differences between using aggregate () and find () in MongoDB?

With aggregate + $match, you get a big monolithic BSON containing all matching documents. With find, you get a cursor to all matching documents. Then you can get each document one by one.

Which aggregate method is preferred for use by MongoDB?

The pipeline provides efficient data aggregation using native operations within MongoDB, and is the preferred method for data aggregation in MongoDB. The aggregation pipeline can operate on a sharded collection. The aggregation pipeline can use indexes to improve its performance during some of its stages.


1 Answers

$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

like image 74
Shaishab Roy Avatar answered Sep 20 '22 09:09

Shaishab Roy