Nearby lessons
19 of 22🔤 MongoDB: Type Conversion and Checking Operators
MongoDB provides several type conversion and type checking operators
to transform field data types and verify data formats inside aggregation pipelines.
These operators are extremely useful when working with inconsistent or mixed-type data.
📚 Sample Collection Setup
Example01
1️⃣ Using $toString — Convert to String
Example02
2️⃣ Using $toInt — Convert to Integer
Example03
3️⃣ Using $toLong — Convert to 64-bit Integer
Example04
4️⃣ Using $toDouble — Convert to Floating-Point
Example05
5️⃣ Using $toDecimal — Convert to Decimal128
Example06
6️⃣ Using $toBool — Convert to Boolean
Example07
7️⃣ Using $toDate — Convert to Date
Example08
8️⃣ Using $toObjectId — Convert to ObjectId
Example09
9️⃣ Using $convert — Safe Type Conversion
Example10
🔟 Using $type — Check BSON Type
Example11
1️⃣1️⃣ Using $isNumber — Numeric Check
Example12