I have this query:
const match: PipelineStage.Match = {
$match: {
deleted: false,
provinceId: new mongoose.Types.ObjectId(req.params.provinceId),
},
};
const query: PipelineStage[] = [match];
const cities = await cityModel.aggregate(query);
But the problem is neither boolean nor ObjectId is supported by type Expression in the type Match. So I have to use an expression to have a simple match for the type Boolean.
Match interface:
export namespace PipelineStage {
export interface Match {
$match: Expression | Record<string, Expression>;
}
}
The error I receive:
error TS2352: Conversion of type '{ $match: { deleted: boolean; provinceId: ObjectId; }; }[]' to type 'PipelineStage[]' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first. Type '{ $match: { deleted: boolean; provinceId: mongoose.Types.ObjectId; }; }' is not comparable to type 'PipelineStage'. Type '{ $match: { deleted: boolean; provinceId: mongoose.Types.ObjectId; }; }' is not comparable to type 'Match'. Types of property '$match' are incompatible. Type '{ deleted: boolean; provinceId: mongoose.Types.ObjectId; }' is not comparable to type 'Expression | Record<string, Expression>'. Type '{ deleted: boolean; provinceId: mongoose.Types.ObjectId; }' is not comparable to type 'Record<string, Expression>'. Property 'deleted' is incompatible with index signature. Type 'boolean' is not comparable to type 'Expression'.
So the question is how can I use simple matching for a boolean, number, or ObjectId in TypeScript aggregate?
CodePudding user response:
Using Filterquery
as the match object type solved the problem.
const match: PipelineStage.Match = {
$match: {
deleted: false,
provinceId: new mongoose.Types.ObjectId(req.params.provinceId),
} as FilterQuery<interfaces.ICity>,
};
const query: PipelineStage[] = [match];
const cities = await cityModel.aggregate(query);