NestJs Sample RegExp

if (searchCityDto.hasOwnProperty('title')) {
Object.assign(filter, {
"title.fa": new RegExp(searchCityDto.title, 'i'),
});
cacheKey += '_'+ searchCityDto.title;
}
if (searchCityDto.hasOwnProperty('title')) {
Object.assign(filter, {
"title.fa": new RegExp('^'+searchCityDto.title, 'i'),
});
cacheKey += '_'+ searchCityDto.title;
}
/**
* get all city for one country
*
@param searchStateDto
*/
async cities(searchCityDto: SearchCityDto) {
let filter = {};

Object.assign(filter, { 'topicType': LocationTopicTypeEnum.CITY});

let cacheKey = 'search_in_cities';

if (searchCityDto.hasOwnProperty('countryId')) {
Object.assign(filter, { 'countryId': searchCityDto.countryId});
cacheKey += '_'+ searchCityDto.countryId;
}

if (searchCityDto.hasOwnProperty('stateId')) {
Object.assign(filter, { 'stateId': searchCityDto.stateId});
cacheKey += '_'+ searchCityDto.stateId;
}

if (searchCityDto.hasOwnProperty('title')) {
Object.assign(filter, {
"title.fa": new RegExp('^'+searchCityDto.title, 'i'),
});
cacheKey += '_'+ searchCityDto.title;
}

/**
* get cache
*/
let getCache = await this.cacheManager.get(cacheKey);
if(!getCache){
/**
* find Db
*/
let data = await this.locationModel.find(filter);
/**
* set cache for 1 day = 86400 second
*/
await this.cacheManager.set(cacheKey,[...new Set(data)], {ttl:86400});

getCache = data;


}

return getCache;
}
if (searchUserDto.hasOwnProperty('fullName')) {
searchUserDto.fullName = this._strReplaceForArabic(searchUserDto.fullName);

let tempFullName = searchUserDto.fullName;
let fullNameWithOutSpace = tempFullName.replace( " ", '');

arrConditionAnd.push(

{
"$expr": {
"$or":[
{
"$regexMatch": {
"input": {"$concat": ["$firstName", " ", "$lastName"]},
"regex": searchUserDto.fullName.toString(),
"options": "i"
}
},
{
"$regexMatch": {
"input": "$firstName",
"regex": searchUserDto.fullName.toString(),
"options": "i"
}
},
{
"$regexMatch": {
"input": "$lastName",
"regex": searchUserDto.fullName.toString(),
"options": "i"
}
},
{
"$regexMatch": {
"input": {"$replaceAll": { input: "$firstName", find: " ", replacement: ""}},
"regex": searchUserDto.fullName.toString(),
"options": "i"
}
},
{
"$regexMatch": {
"input": {"$replaceAll": { input: "$firstName", find: "‌", replacement: ""}},
"regex": searchUserDto.fullName.toString(),
"options": "i"
}
},
{
"$regexMatch": {
"input": {"$replaceAll": { input: "$lastName", find: " ", replacement: ""}},
"regex": searchUserDto.fullName.toString(),
"options": "i"
}
},
{
"$regexMatch": {
"input": {"$replaceAll": { input: "$lastName", find: "‌", replacement: ""}},
"regex": searchUserDto.fullName.toString(),
"options": "i"
}
},
{
"$regexMatch": {
"input": "$firstName",
"regex": fullNameWithOutSpace,
"options": "i"
}
},
{
"$regexMatch": {
"input": "$lastName",
"regex": fullNameWithOutSpace,
"options": "i"
}
},
{
"$regexMatch": {
"input": {"$concat": ["$firstName", " ", "$lastName"]},
"regex": fullNameWithOutSpace,
"options": "i"
}
},
]

}
}
);
}
let tempFullName         =  searchUserDto.fullName;
let fullNameWithOutSpace = tempFullName.replace( new RegExp(" ", 'g'), '')
.replace( new RegExp("‌", 'g'), '');

arrConditionAnd.push(

{
"$expr": {
"$or":[
{
"$regexMatch": {
"input": {"$concat": ["$firstName", " ", "$lastName"]},
"regex": searchUserDto.fullName.toString(),
"options": "i"
}
},
{
"$regexMatch": {
"input": {"$concat": [{"$replaceAll": { input: "$firstName", find: " ", replacement: ""}}, " ", "$lastName"]},
"regex": searchUserDto.fullName.toString(),
"options": "i"
}
},
{
"$regexMatch": {
"input": {"$concat": [{"$replaceAll": { input: "$firstName", find: "‌", replacement: ""}}, " ", "$lastName"]},
"regex": searchUserDto.fullName.toString(),
"options": "i"
}
},
{
"$regexMatch": {
"input": {"$concat": ["$firstName", " ", {"$replaceAll": { input: "$lastName", find: " ", replacement: ""}}]},
"regex": searchUserDto.fullName.toString(),
"options": "i"
}
},
{
"$regexMatch": {
"input": {"$concat": ["$firstName", " ", {"$replaceAll": { input: "$lastName", find: "‌", replacement: ""}}]},
"regex": searchUserDto.fullName.toString(),
"options": "i"
}
},
{
"$regexMatch": {
"input": {"$concat": [{"$replaceAll": { input: "$firstName", find: " ", replacement: ""}}, " ", {"$replaceAll": { input: "$lastName", find: " ", replacement: ""}}]},
"regex": searchUserDto.fullName.toString(),
"options": "i"
}
},
{
"$regexMatch": {
"input": {"$concat": [{"$replaceAll": { input: "$firstName", find: "‌", replacement: ""}}, " ", {"$replaceAll": { input: "$lastName", find: "‌", replacement: ""}}]},
"regex": searchUserDto.fullName.toString(),
"options": "i"
}
},
{
"$regexMatch": {
"input": "$firstName",
"regex": searchUserDto.fullName.toString(),
"options": "i"
}
},
{
"$regexMatch": {
"input": "$lastName",
"regex": searchUserDto.fullName.toString(),
"options": "i"
}
},
{
"$regexMatch": {
"input": {"$replaceAll": { input: "$firstName", find: " ", replacement: ""}},
"regex": searchUserDto.fullName.toString(),
"options": "i"
}
},
{
"$regexMatch": {
"input": {"$replaceAll": { input: "$firstName", find: "‌", replacement: ""}},
"regex": searchUserDto.fullName.toString(),
"options": "i"
}
},
{
"$regexMatch": {
"input": {"$replaceAll": { input: "$lastName", find: " ", replacement: ""}},
"regex": searchUserDto.fullName.toString(),
"options": "i"
}
},
{
"$regexMatch": {
"input": {"$replaceAll": { input: "$lastName", find: "‌", replacement: ""}},
"regex": searchUserDto.fullName.toString(),
"options": "i"
}
},
{
"$regexMatch": {
"input": "$firstName",
"regex": fullNameWithOutSpace,
"options": "i"
}
},
{
"$regexMatch": {
"input": "$lastName",
"regex": fullNameWithOutSpace,
"options": "i"
}
},
{
"$regexMatch": {
"input": {"$concat": ["$firstName", " ", "$lastName"]},
"regex": fullNameWithOutSpace,
"options": "i"
}
},
{
"$regexMatch": {
"input": {"$concat": [{"$replaceAll": { input: "$firstName", find: "‌", replacement: ""}}, {"$replaceAll": { input: "$lastName", find: "‌", replacement: ""}}]},
"regex": fullNameWithOutSpace,
"options": "i"
}
},
{
"$regexMatch": {
"input": {"$concat": [{"$replaceAll": { input: "$firstName", find: " ", replacement: ""}}, {"$replaceAll": { input: "$lastName", find: " ", replacement: ""}}]},
"regex": fullNameWithOutSpace,
"options": "i"
}
},
]

}
}
);

}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store