Skip to content

Commit 0db3a6f

Browse files
fix: Parse.Query.containedIn and matchesQuery do not work with nested objects (#9738)
1 parent 3271e2a commit 0db3a6f

File tree

2 files changed

+74
-3
lines changed

2 files changed

+74
-3
lines changed

spec/ParseQuery.spec.js

+68
Original file line numberDiff line numberDiff line change
@@ -5306,4 +5306,72 @@ describe('Parse.Query testing', () => {
53065306
expect(score).toEqual([1]);
53075307
}, { useMasterKey: true });
53085308
});
5309+
5310+
describe_only_db('mongo')('query nested keys', () => {
5311+
it('queries nested key using equalTo', async () => {
5312+
const child = new Parse.Object('Child');
5313+
child.set('key', 'value');
5314+
await child.save();
5315+
5316+
const parent = new Parse.Object('Parent');
5317+
parent.set('some', {
5318+
nested: {
5319+
key: {
5320+
child,
5321+
},
5322+
},
5323+
});
5324+
await parent.save();
5325+
5326+
const query1 = await new Parse.Query('Parent')
5327+
.equalTo('some.nested.key.child', child)
5328+
.find();
5329+
5330+
expect(query1.length).toEqual(1);
5331+
});
5332+
5333+
it('queries nested key using containedIn', async () => {
5334+
const child = new Parse.Object('Child');
5335+
child.set('key', 'value');
5336+
await child.save();
5337+
5338+
const parent = new Parse.Object('Parent');
5339+
parent.set('some', {
5340+
nested: {
5341+
key: {
5342+
child,
5343+
},
5344+
},
5345+
});
5346+
await parent.save();
5347+
5348+
const query1 = await new Parse.Query('Parent')
5349+
.containedIn('some.nested.key.child', [child])
5350+
.find();
5351+
5352+
expect(query1.length).toEqual(1);
5353+
});
5354+
5355+
it('queries nested key using matchesQuery', async () => {
5356+
const child = new Parse.Object('Child');
5357+
child.set('key', 'value');
5358+
await child.save();
5359+
5360+
const parent = new Parse.Object('Parent');
5361+
parent.set('some', {
5362+
nested: {
5363+
key: {
5364+
child,
5365+
},
5366+
},
5367+
});
5368+
await parent.save();
5369+
5370+
const query1 = await new Parse.Query('Parent')
5371+
.matchesQuery('some.nested.key.child', new Parse.Query('Child').equalTo('key', 'value'))
5372+
.find();
5373+
5374+
expect(query1.length).toEqual(1);
5375+
});
5376+
});
53095377
});

src/Adapters/Storage/Mongo/MongoTransform.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ function transformQueryKeyValue(className, key, value, schema, count = false) {
327327
}
328328

329329
// Handle query constraints
330-
const transformedConstraint = transformConstraint(value, field, count);
330+
const transformedConstraint = transformConstraint(value, field, key, count);
331331
if (transformedConstraint !== CannotTransform) {
332332
if (transformedConstraint.$text) {
333333
return { key: '$text', value: transformedConstraint.$text };
@@ -651,12 +651,15 @@ function transformTopLevelAtom(atom, field) {
651651
// If it is not a valid constraint but it could be a valid something
652652
// else, return CannotTransform.
653653
// inArray is whether this is an array field.
654-
function transformConstraint(constraint, field, count = false) {
654+
function transformConstraint(constraint, field, queryKey, count = false) {
655655
const inArray = field && field.type && field.type === 'Array';
656+
// Check wether the given key has `.`
657+
const isNestedKey = queryKey.indexOf('.') > -1;
656658
if (typeof constraint !== 'object' || !constraint) {
657659
return CannotTransform;
658660
}
659-
const transformFunction = inArray ? transformInteriorAtom : transformTopLevelAtom;
661+
// For inArray or nested key, we need to transform the interior atom
662+
const transformFunction = (inArray || isNestedKey) ? transformInteriorAtom : transformTopLevelAtom;
660663
const transformer = atom => {
661664
const result = transformFunction(atom, field);
662665
if (result === CannotTransform) {

0 commit comments

Comments
 (0)