Saturday 31 August 2019

MongoDB operators with examples

MongoDB operators with examples

Comparison Query Selectors

$eq  Equal to a specified value.
$gt  Greater than a specified value.
$gte  Greater than or equal to a specified value.
$lt  Less than a specified value.
$lte  Less than or equal to a specified value.
$ne  Not equal to a specified value.
$in  Matches any of the values specified in an array.
$nin  Matches none of the values specified in an array.

Example:
db.mycollectiontest.find('{"likes": {$gt:10}"likes"}'); 


Logical Query Selectors
$or  Joins query clauses with a logical OR Operator.
$and  Joins query clauses with a logical AND Operator.
$not  Inverts the effect of a query expression.
$nor  Joins query clauses with a logical NOR returns.

Example
db.mycollectiontest.find({"likes": {$gt:10}, $or: [{"by": "tutorials point"})


Element Query Selectors
$exists  Matches documents that have the specified field.
$type  Selects documents if a field is of the specified type.



Evalution Query Selector
$mod  Performs a modulo operation.
$regex  Specified regular expression.
$text  Performs text search.
$where  Matches documents that satisfy a JavaScript expression.



Array Query Selector
$all        Matches arrays that contain all elements specified in the query.
$elemMatch Selects documents if element in the array field matches all the specified $elemMatch conditions.
$size       Selects documents if the array field is a specified size.


Field Update Operators
$inc        Increments the value.
$mul        Multiplies the value.
$rename     Renames a field.
$setOnInsert Sets the value of a field if an update results in an insert of a document.
$set  Sets the value of a field in a document.
$unset  Removes the specified field from a document.
$min  Only updates the field if the specified value is less than the existing field value.
$max  Only updates the field if the specified value is greater than the existing field value.
$currentDate  Sets the value of a field to current date, either as a Date or a Timestamp.