Skip to content

Filter Update Operators

Aryeh Citron edited this page Apr 26, 2026 · 1 revision

Filter & Update Operators

Comparison Operators

Operator Description Example
$eq Equal { "status": "active" } or { "status": { "$eq": "active" } }
$ne Not equal { "status": { "$ne": "deleted" } }
$gt Greater than { "total": { "$gt": 100 } }
$gte Greater than or equal { "total": { "$gte": 100 } }
$lt Less than { "total": { "$lt": 50 } }
$lte Less than or equal { "total": { "$lte": 50 } }
$in Match any value in array { "status": { "$in": ["active", "pending"] } }
$nin Match none in array { "status": { "$nin": ["deleted", "archived"] } }

Logical Operators

Operator Description Example
$and Logical AND { "$and": [{ "a": 1 }, { "b": 2 }] }
$or Logical OR { "$or": [{ "a": 1 }, { "b": 2 }] }
$nor Logical NOR { "$nor": [{ "a": 1 }, { "b": 2 }] }
$not Logical NOT { "price": { "$not": { "$gt": 100 } } }

Element & Type Operators

Operator Description Example
$exists Field exists { "email": { "$exists": true } }
$type Field is specific BSON type { "age": { "$type": "int" } }

Array Operators

Operator Description Example
$all Array contains all values { "tags": { "$all": ["red", "blue"] } }
$elemMatch Array element matches all conditions { "scores": { "$elemMatch": { "$gt": 80, "$lt": 90 } } }
$size Array has specific length { "tags": { "$size": 3 } }

Evaluation Operators

Operator Description Example
$regex Regular expression match { "name": { "$regex": "^A", "$options": "i" } }
$mod Modulo operation { "qty": { "$mod": [4, 0] } }
$expr Aggregation expression { "$expr": { "$gt": ["$qty", "$ordered"] } }
$text Text search { "$text": { "$search": "coffee shop" } }
$jsonSchema JSON Schema validation { "$jsonSchema": { "required": ["name"] } }
$where JavaScript expression (requires JsTriggers) { "$where": "this.a > this.b" }

Bitwise Operators

Operator Description
$bitsAllSet All specified bits are set
$bitsAllClear All specified bits are clear
$bitsAnySet Any specified bit is set
$bitsAnyClear Any specified bit is clear

Geospatial Operators

Operator Description
$geoWithin Within a geometry (with $geometry, $centerSphere)
$geoIntersects Intersects a geometry
$near Near a point (with $maxDistance, $minDistance)
$nearSphere Near a point on a sphere

Update Operators

Field Update Operators

Operator Description Example
$set Set field value { "$set": { "status": "active" } }
$unset Remove field { "$unset": { "temp": "" } }
$inc Increment numeric value { "$inc": { "count": 1 } }
$mul Multiply numeric value { "$mul": { "price": 1.1 } }
$min Update if new value is less { "$min": { "low": 5 } }
$max Update if new value is greater { "$max": { "high": 100 } }
$rename Rename field { "$rename": { "old": "new" } }
$currentDate Set to current date { "$currentDate": { "updatedAt": true } }
$setOnInsert Set on upsert insert only { "$setOnInsert": { "createdAt": "2024-01-01" } }

Array Update Operators

Operator Description Example
$push Add to array { "$push": { "tags": "new" } }
$push + $each Add multiple to array { "$push": { "tags": { "$each": ["a","b"] } } }
$push + $position Insert at position { "$push": { "tags": { "$each": ["a"], "$position": 0 } } }
$push + $sort Sort after push { "$push": { "scores": { "$each": [5], "$sort": -1 } } }
$push + $slice Limit array size { "$push": { "scores": { "$each": [5], "$slice": 10 } } }
$pull Remove matching elements { "$pull": { "tags": "old" } }
$pullAll Remove all specified values { "$pullAll": { "tags": ["a", "b"] } }
$addToSet Add if not present { "$addToSet": { "tags": "unique" } }
$addToSet + $each Add multiple if not present { "$addToSet": { "tags": { "$each": ["a","b"] } } }
$pop Remove first or last element { "$pop": { "tags": 1 } } (last) / { "$pop": { "tags": -1 } } (first)

Bitwise Update Operator

Operator Description
$bit Bitwise AND, OR, XOR update

See Also

Clone this wiki locally