Search like number field in MongoDb by NestJs
Aug 13, 2022
let aggregateTotal = [];
let aggregateMatch = {};
if (searchServiceDto.hasOwnProperty('code')) {
Object.assign(aggregateMatch, {
codeStr: new RegExp(searchServiceDto.code.toString()),
});
}
aggregateTotal.push(
{
$addFields : {
codeStr: {
$toString: '$code'
}
}
}
)
aggregateTotal.push({
$match: aggregateMatch,
});
aggregateTotal.push(
{
$project : {
codeStr: 0
}
}
)let data = await this.serviceSoftModel.aggregate(aggregateTotal);
type code in MongoDb is number