diff --git a/src/runtime/query/match/index.ts b/src/runtime/query/match/index.ts index e5cd50bf8..f82761f71 100644 --- a/src/runtime/query/match/index.ts +++ b/src/runtime/query/match/index.ts @@ -69,7 +69,9 @@ function createOperators (match: (...args: any[]) => boolean, operators: Record< /** * Match if item is in condition array **/ - $in: (item, condition) => ensureArray(condition).some(cond => match(item, cond)), + $in: (item, condition) => ensureArray(condition).some( + cond => Array.isArray(item) ? match(item, { $contains: cond }) : match(item, cond) + ), /** * Match if item contains every condition or math every rule in condition array diff --git a/test/features/query/match.test.ts b/test/features/query/match.test.ts index 1ce61d43d..30b5e5013 100644 --- a/test/features/query/match.test.ts +++ b/test/features/query/match.test.ts @@ -74,17 +74,6 @@ describe('Match', () => { }) }) - test('$in', () => { - const item = { id: 1, name: 'a', to: 'a', category: 'c1', nested: { users: ['Mahatma', 'Steve', 'Woodrow'] } } - - expect(match(item, { name: { $in: ['a', 'b'] } })).toBe(true) - expect(match(item, { category: { $in: 'c1' } })).toBe(true) - expect(match(item, { category: { $in: ['c1', 'c2'] } })).toBe(true) - expect(match(item, { category: { $in: ['c2', 'c3'] } })).toBe(false) - - expect(match(item, { id: { $in: [1, 2] } })).toBe(true) - }) - test('$eq', () => { // string expect(match(item, { name: { $eq: 'a' } })).toBe(true) @@ -187,4 +176,42 @@ describe('Match', () => { expect(match(item, { id: { $lte: 0 } })).toBe(false) }) }) + + describe('$in ', () => { + test('string filed', () => { + const item = { id: 1, name: 'a', to: 'a', category: 'c1', nested: { users: ['Mahatma', 'Steve', 'Woodrow'] } } + + expect(match(item, { name: { $in: ['a', 'b'] } })).toBe(true) + expect(match(item, { category: { $in: 'c1' } })).toBe(true) + expect(match(item, { category: { $in: ['c1', 'c2'] } })).toBe(true) + expect(match(item, { category: { $in: ['c2', 'c3'] } })).toBe(false) + + expect(match(item, { id: { $in: [1, 2] } })).toBe(true) + }) + + test('array field', () => { + const data = [ + { + name: 'post1', + tags: ['school', 'office'] + }, + { + name: 'post2', + tags: ['school', 'home'] + }, + { + item: 'Maps', + tags: ['office', 'storage'] + } + ] + + const condition = { tags: { $in: ['school', 'home'] } } + data.forEach((item) => { + expect(match(item, condition)).toBe( + item.tags.includes(condition.tags.$in[0]) || + item.tags.includes(condition.tags.$in[1]) + ) + }) + }) + }) })