Exercises: Type mismatches
Atlas Search is quite type sensitive. The BSON type of each field is used to determine which index mapping type to use. And at query time, the search operator type of the value
is used to determine which indexed type to query for a particular field/path name.
booleanβ
π Playground link: boolean type mismatchWhy doesn't this playground match as expected?
Explanation
The type of $search.equals.value
is not an actual boolean (it's a string
of "true") and thus will be looking for an indexed value corresponding that type (which in this case would be a token
field type).
Here's a corrected pipeline:
[
{
$search: {
index: "default",
equals: {
value: true,
path: "in_stock"
}
}
}
]
numberβ
π Playground link: number type mismatchWhy won't the query match?
Solution
The value must be a number, not a string.
value: 10.99
ObjectIDβ
π Playground link: ObjectId type mismatchThe ObjectId value is correct, but the document does not match. Can you spot, and fix, the issue?
Solution
The value must be an ObjectId, not a string.
value: ObjectId("5a9427648b0beebeb69589a1")