Skip to content

Commit adc61d0

Browse files
committed
Fixed $elemMatch operator handling
1 parent d7d94a0 commit adc61d0

File tree

3 files changed

+12
-5
lines changed

3 files changed

+12
-5
lines changed

src/packages/pongo/src/e2e/compatibilityTest.e2e.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ void describe('MongoDB Compatibility Tests', () => {
464464
);
465465
});
466466

467-
void it.skip('should find documents with a nested array element match filter in both PostgreSQL and MongoDB', async () => {
467+
void it('should find documents with a nested array element match filter in both PostgreSQL and MongoDB', async () => {
468468
const pongoCollection = pongoDb.collection<User>('testCollection');
469469
const mongoCollection = mongoDb.collection<User>('testCollection');
470470

src/packages/pongo/src/postgres/filter/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const constructComplexFilterQuery = (
2222
.map(
2323
([nestedKey, val]) =>
2424
isEquality
25-
? handleOperator(`${key}.${nestedKey}`, '$eq', val) //regular value
25+
? handleOperator(`${key}.${nestedKey}`, '$eq', val) // regular value
2626
: handleOperator(key, nestedKey, val), // operator
2727
)
2828
.join(` ${AND} `);

src/packages/pongo/src/postgres/filter/queryOperators.ts

+10-3
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,18 @@ export const handleOperator = (
4848
`{${path.split('.').join(',')}}`,
4949
(value as unknown[]).map((v) => format('%L', v)).join(', '),
5050
);
51-
case '$elemMatch':
51+
case '$elemMatch': {
52+
const subQuery = Object.entries(value as Record<string, unknown>)
53+
.map(([subKey, subValue]) =>
54+
format(`@."%s" == %s`, subKey, JSON.stringify(subValue)),
55+
)
56+
.join(' && ');
5257
return format(
53-
'data @> %L::jsonb',
54-
JSON.stringify(buildNestedObject(path, { $elemMatch: value })),
58+
`jsonb_path_exists(data, '$.%s[*] ? (%s)')`,
59+
path,
60+
subQuery,
5561
);
62+
}
5663
case '$all':
5764
return format(
5865
'data @> %L::jsonb',

0 commit comments

Comments
 (0)