Merge pull request #2855 from payloadcms/fix/#2832
fix: #2832, slow like queries with lots of records
This commit is contained in:
@@ -372,6 +372,23 @@ export class ParamParser {
|
||||
}
|
||||
}
|
||||
|
||||
if (operator === 'like' && typeof formattedValue === 'string') {
|
||||
const words = formattedValue.split(' ');
|
||||
|
||||
const result = {
|
||||
value: {
|
||||
$and: words.map((word) => ({
|
||||
[path]: {
|
||||
$regex: word.replace(/[\\^$*+?\\.()|[\]{}]/g, '\\$&'),
|
||||
$options: 'i',
|
||||
},
|
||||
})),
|
||||
},
|
||||
};
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// Some operators like 'near' need to define a full query
|
||||
// so if there is no operator key, just return the value
|
||||
if (!operatorKey) {
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import mongoose from 'mongoose';
|
||||
import { createArrayFromCommaDelineated } from './createArrayFromCommaDelineated';
|
||||
import wordBoundariesRegex from '../utilities/wordBoundariesRegex';
|
||||
import { Field, TabAsField } from '../fields/config/types';
|
||||
|
||||
type SanitizeQueryValueArgs = {
|
||||
@@ -107,11 +106,6 @@ export const sanitizeQueryValue = ({ field, path, operator, val, hasCustomID }:
|
||||
if (operator === 'contains') {
|
||||
formattedValue = { $regex: formattedValue, $options: 'i' };
|
||||
}
|
||||
|
||||
if (operator === 'like' && typeof formattedValue === 'string') {
|
||||
const $regex = wordBoundariesRegex(formattedValue);
|
||||
formattedValue = { $regex };
|
||||
}
|
||||
}
|
||||
|
||||
if (operator === 'exists') {
|
||||
|
||||
Reference in New Issue
Block a user