I have the following function in my database layer:
public async findByFilter(filter: Partial<InterfaceMongooseModel>): Promise<InterfaceMongooseModel> {
return MongooseModel.findOne(filter);
}
This works for most cases. However, if I have the following filter:
{
property: value,
createdDate: { $gte: oneDayAgo },
}
I will have a type error.
The best solution I found so far is this:
public async findByFilter(filter: object): Promise<InterfaceMongooseModel> {
return MongooseModel.findOne(filter);
}
However, this is too generic and doesn’t provide much information. I wander if there’s anything more specific to use in this case.
What did you try and what were you expecting?
I would like to have a proper type for the argument in a mongoDB query.
2
Answers
Mongoose has the
FilterQuery
type for filter queries. This is the proper usage in this case:Thanks @cmgchess for pointing this out.
The mongodb store the date with format
YYYY-MM-DDTHH:MM:SS.SSSZ
and i think you are passing the date with wrong format to apply filters.
pass the date with correct format