Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using IF/ELSE in MongoDB

Tags:

mongodb

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") } }
like image 902
N Raghu Avatar asked Jul 02 '16 09:07

N Raghu


People also ask

Is there a $in in MongoDB?

MongoDB provides different types of comparison query operators and $in operator is one of them. This operator is used to select those documents where the value of the field is equal to any of the given value in the array.

Which are the conditional operators in MongoDB?

MongoDB Update Operators A conditional operator compares two expressions and fetched documents from mongodb collection. In this page we are going to discuss about the conditional operators and usage of conditional operators. Our database name is 'myinfo' and our collection name is 'testtable'.

What is $GTE in MongoDB?

$gte selects the documents where the value of the field is greater than or equal to (i.e. >= ) a specified value (e.g. value .) For most data types, comparison operators only perform comparisons on fields where the BSON type matches the query value's type.

What is $project in MongoDB?

The $project takes a document that can specify the inclusion of fields, the suppression of the _id field, the addition of new fields, and the resetting of the values of existing fields. Alternatively, you may specify the exclusion of fields. Specifies the inclusion of a field.


1 Answers

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'); }
like image 158
DAXaholic Avatar answered Nov 15 '22 05:11

DAXaholic