5

Can someone help to achieve IF/ELSE in mongoDB? I use below code in SQL Server.

DECLARE @p =1
IF @p =1
PRINT @p
ELSE PRINT "NO"

I tried the below codes in MongoDB but could not succeed.

var p=1
{ $cond: { if: p=1, then: print(p), else: print("NO") } }
var p=1
{ $cond: { if: p=1, print(p), print("NO") } }

1 Answer 1

8

Do you mean the MongoDB shell? If yes then just use JavaScript like so

var p = 1;
if (p == 1) { print(p); } else { print('NO'); }
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.