Inspecting BSON Types with $type and $isNumber
MongoDB provides query and aggregation operators that let you inspect the BSON type of a field. These operators are especially valuable when cleaning inconsistent historical data or diagnosing why a filter returns unexpected results.
Query operator: $type
The query $type operator matches documents where the field has a specified BSON type.
For example:
db.products.find({ price: { $type: "string" } });
This finds documents where price is a string.
Another example:
db.products.find({ createdAt: { $type: "date" } });
This finds documents whose createdAt field is a BSON Date.
For array fields, the query $type operator has special behavior: it can match an array containing an element of the specified type. Therefore, a $type query should not automatically be interpreted as proof that the top-level field itself is a scalar of that type.
Aggregation expression: $type
The aggregation $type expression returns a string describing the type of a value.
db.products.aggregate([
{
$project: {
name: 1,
priceType: { $type: "$price" }
}
}
]);
The aggregation expression can return missing when the referenced field does not exist. This is useful when distinguishing missing fields from explicit null values.
Numeric type checks
The aggregation $isNumber expression returns true when the expression evaluates to a numeric BSON type.
db.products.aggregate([
{
$project: {
name: 1,
price: 1,
priceIsNumber: { $isNumber: "$price" }
}
}
]);
This helps detect values that are numeric versus values that are strings or other BSON types.
Finding inconsistent values
Suppose the collection contains:
{ name: "A", price: 100 }
{ name: "B", price: "200" }
{ name: "C", price: NumberDecimal("300.00") }
{ name: "D" }
A numeric type audit can identify the string-valued price and missing price field.
Do not immediately convert every unexpected value. First determine whether the value is valid, whether the conversion is safe, and whether the original information needs to be preserved for auditing.
BSON type names and aliases
MongoDB supports BSON type names and corresponding numeric aliases in appropriate contexts. Prefer readable string names in most application queries because they make the intended type easier to understand.
db.typeAudit.insertMany([
{ name: "A", price: 100 },
{ name: "B", price: "200" },
{ name: "C", price: NumberDecimal("300.00") },
{ name: "D" },
{ name: "E", price: null }
]);
// Find string-valued prices.
db.typeAudit.find({ price: { $type: "string" } });
// Inspect each document's price type.
db.typeAudit.aggregate([
{
$project: {
name: 1,
price: 1,
priceType: { $type: "$price" },
priceIsNumber: { $isNumber: "$price" }
}
}
]);$type operator finds string-valued prices, while the aggregation $type expression reports the type of each value. $isNumber identifies numeric BSON values. Inspecting the data first is an important step before performing a migration.- The query
$typeoperator can identify documents by BSON type. - The aggregation
$typeexpression reports the type of an expression. - The aggregation
$typeexpression can reportmissingfor absent fields. $isNumberhelps identify numeric BSON values.- Inspect inconsistent data before attempting type conversion.
Which aggregation expression helps determine whether a field contains a numeric BSON value?