Skip to main content

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 mismatch

Why 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 mismatch

Why won't the query match?

Solution

The value must be a number, not a string.

value: 10.99

ObjectID​

πŸ”— Playground link: ObjectId type mismatch

The 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")