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") } }
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.
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'.
$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.
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.
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'); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With