pubkey / rxdb

A fast, local first, reactive Database for JavaScript Applications https://rxdb.info/
https://rxdb.info/
Apache License 2.0
21.37k stars 1.04k forks source link

regex MongoDB expressions supported? #34

Closed cgatian closed 7 years ago

cgatian commented 7 years ago

I'm a beginner to PouchDB and Mongo so I could be consuming this library incorrectly.

When I run the following query I return the correct number of results.

        collection.find()
          .where({ name: { $eq: 'Bob' } })
          .$
          .filter(docs => docs !== null)
          .subscribe(docs => {
            docs.map(doc => console.log(doc.get('name')));
          });

If I modify the where clause to use a regex all results are returned.

 .where({ name: { $regex: /Bob/ig } })

I'm referencing this documentation for the query syntax.

Thank you.

pubkey commented 7 years ago

Hi @cgatian . The query-engine depends on pouch-find which supports the regex-syntax. See Regex with pouch-find.

I will make a test for this tomorrow so that I can tell you if its definitely working. Could you post your schema please to make sure it has nothing to do with it?

cgatian commented 7 years ago

Thanks for the quick response. I'll test tomorrow morning. Thank you

cgatian commented 7 years ago

@pubkey Hmm still no luck. Im using the hero schema found in the example apps.

    RxDB.plugin(require('pouchdb-adapter-idb'));
    RxDB.plugin(require('pouchdb-adapter-http'));
    RxDB.plugin(require('pouchdb-replication'));
    RxDB.plugin(require('pouchdb-find'));

    this.db$ = Observable.fromPromise(
      RxDB.create('heroesDB', 'idb', 'myLongAndStupidPassword', true)  // create database
        .then(db => db));

    this.col$ =
      this.db$
      .mergeMap(db => db.collection('hero', heroSchema))
        .filter(col => !!col)
      .map(col => {
        let c: RxCollection = <RxCollection>col;
        c.sync(`http://localhost:5984/hero/`);
        c.find({ name: { $regex: /c.*/ } })
          .$
          .filter(docs => docs !== null)
          .subscribe(docs => {
            docs.map(doc => console.log(doc.get('name')));
          });
pubkey commented 7 years ago

Hi @cgatian I added some tests for the .regex()-function. See here

The regex basically works, but you cannot use $regex on the primary-field by the mango-query-definition. See here

I also added a check so it throws when you call regex() on the primary-field.