fix: array operator query error (#217)

This commit is contained in:
ChengLei Shao 2022-03-03 12:09:25 +08:00 committed by GitHub
parent 18b4feb104
commit 4e9baf3957
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 2 deletions

View File

@ -152,6 +152,21 @@ describe('array field operator', function () {
expect(filter2[0].get('name')).toEqual(t2.get('name')); expect(filter2[0].get('name')).toEqual(t2.get('name'));
}); });
test('$anyOf with $and', async () => {
const filter3 = await Test.repository.find({
filter: {
$and: [
{
'selected.$anyOf': ['aa'],
},
],
},
});
expect(filter3.length).toEqual(1);
expect(filter3[0].get('name')).toEqual(t2.get('name'));
});
test('$anyOf', async () => { test('$anyOf', async () => {
const filter3 = await Test.repository.find({ const filter3 = await Test.repository.find({
filter: { filter: {

View File

@ -121,10 +121,12 @@ export default class FilterParser {
} else if (typeof opKey === 'function') { } else if (typeof opKey === 'function') {
skipPrefix = origins.join('.'); skipPrefix = origins.join('.');
value = opKey(lodash.get(unflatten(originalFiler), skipPrefix), { const queryValue = lodash.get(unflatten(originalFiler), skipPrefix);
value = opKey(queryValue, {
db: this.database, db: this.database,
path: skipPrefix, path: skipPrefix,
fieldName: skipPrefix.replace(`.${firstKey}`, ''), fieldName: this.getFieldNameFromQueryPath(skipPrefix),
model: this.model, model: this.model,
}); });
break; break;
@ -220,4 +222,18 @@ export default class FilterParser {
debug('where %o, include %o', where, include); debug('where %o, include %o', where, include);
return { where, include: toInclude(include) }; return { where, include: toInclude(include) };
} }
private getFieldNameFromQueryPath(queryPath: string) {
const paths = queryPath.split('.');
let fieldName;
for (const path of paths) {
if (path.startsWith('$') || !lodash.isNaN(parseInt(path))) {
continue;
}
fieldName = path;
}
return fieldName;
}
} }