diff --git a/src/modules/key-compression.js b/src/modules/key-compression.js index 55ba4d62278..7525a2af6df 100644 --- a/src/modules/key-compression.js +++ b/src/modules/key-compression.js @@ -177,8 +177,21 @@ class KeyCompressor { const selector = {}; Object.keys(queryJSON.selector).forEach(key => { const value = queryJSON.selector[key]; - const transKey = this._transformKey('', '', key.split('.')); - selector[transKey] = value; + if (key.startsWith('$')) { + // $or, $not etc have different structure + const setObj = value.map(obj => { + const newObj = {}; + Object.keys(obj).forEach(k => { + const transKey = this._transformKey('', '', k.split('.')); + newObj[transKey] = obj[k]; + }); + return newObj; + }); + selector[key] = setObj; + } else { + const transKey = this._transformKey('', '', key.split('.')); + selector[transKey] = value; + } }); queryJSON.selector = selector; diff --git a/test/unit/RxCollection.test.js b/test/unit/RxCollection.test.js index f09e8d71138..25411b466b9 100644 --- a/test/unit/RxCollection.test.js +++ b/test/unit/RxCollection.test.js @@ -1,6 +1,7 @@ import assert from 'assert'; import memdown from 'memdown'; import randomInt from 'random-int'; +import randomToken from 'random-token'; import clone from 'clone'; import platform from 'detect-browser'; import AsyncTestUtil from 'async-test-util'; @@ -493,6 +494,35 @@ describe('RxCollection.test.js', () => { }); describe('negative', () => {}); }); + describe('.or()', () => { + it('should find the 2 documents with the or-method', async() => { + const c = await humansCollection.create(10); + // add 2 docs to be found + await c.insert({ + passportId: randomToken(12), + firstName: 'foobarAlice', + lastName: 'aliceLastName', + age: randomInt(10, 50) + }); + await c.insert({ + passportId: randomToken(12), + firstName: 'foobarBob', + lastName: 'bobLastName', + age: randomInt(10, 50) + }); + const query = c.find().or([{ + firstName: 'foobarAlice' + }, { + firstName: 'foobarBob' + }]); + const results = await query.exec(); + assert.equal(results.length, 2); + const foundFirstNames = results.map(doc => doc.firstName); + assert.ok(foundFirstNames.includes('foobarAlice')); + assert.ok(foundFirstNames.includes('foobarBob')); + c.database.destroy(); + }); + }); describe('.sort()', () => { describe('positive', () => { it('sort by age desc (with own index-search)', async() => { @@ -755,7 +785,6 @@ describe('RxCollection.test.js', () => { }); }); }); - describe('.regex()', () => { describe('positive', () => { it('find the one where the regex matches', async() => { @@ -798,7 +827,6 @@ describe('RxCollection.test.js', () => { }); }); }); - describe('.remove()', () => { it('should remove all documents', async() => { const c = await humansCollection.create(10);