1

I need to find record by using aggregate query, and if any of given input is empty array then need to skip that query. In the below code, i have give one empty array to "brandFilter", then that query need to skip $in execution. If it has some array [one, two], then it will give related matched output

Example

{ $filter: {
                    input: '$products',
                    as: 'item',
                    cond: {  $and: [
                        {
                            $gte: ['$$item.prodprice', Number(price)],
                        },
                        { $in: [ "$$item.brand", brandArr ] }  
                    ]}
                }}

1 Answer 1

1

The following filter would get us the expected output:

{
    $filter:{
        "input":"$products",
        "as":"item",
        "cond":{
            $and:[
                {
                    $gte: ['$$item.prodprice', Number(price)]
                },
                {
                    $or:[
                        {
                            $eq:[brandArr[0],undefined]
                        },
                        {
                            $in: [ "$$item.brand", brandArr ]
                        }
                    ]
                }
            ]
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.