1

I need to add condition to find depends to variable value : if it is null don't add to condition if not null - add. I am new in mongodb and for example I gen get it from SQL

DECLARE Variable int = null
SELECT * FROM Table
WHERE field = isnull(Variable ,field)

In mongodb I try to do find

let Variable = "aa";
db.Table.find({
  
         $or: [{field:Variable },{Variable : null }]
         
})

But its don't work as expected Please assist

1 Answer 1

1

Use { [Variable] : null }

db.Table.find({
   $or: [{field:Variable },{ [Variable] : null }]       
})

let Variable = "aa";

const obj = { [Variable] : null };

console.log(obj);

Sign up to request clarification or add additional context in comments.

5 Comments

Could you please write whole script ?
I need second condition in OR true when value of Variable is null

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.