SapphireDb logo SapphireDb

Filter data - Where prefilter

You can use the where-prefilter to filter the data the server returns.
The syntax is a bit tricky because the query has to get converted to an sql expression to increase performance.

Basic query

In this small example only values that start with testV are queried.

\f:(typescript:Angular) export class DemoComponent implements OnInit {\n \tvalues$: Observable<any>;\n\n \tconstructor(private db: SapphireDbService) { }\n\n \tngOnInit() {\n \t\tthis.values$ = this.db.collection('entries')}\n \t\t\t.where(['content', 'StartsWith', 'testV'])\n \t\t\t.values();\n \t}\n }
[]

Syntax

If you want to define your queries using an array use this syntax.

A basic condition is a tuple of three elements: [ key, operation, compare value ]
Example: You want to check a single field to equal your value: ['field', '==', 'value']

To combine multiple condition tuples you can wrap them in another tuple of three or more elements: [condition1, 'and/or', condition2]
Example: You want to check if the field value equals 'value' or 'value2': [['field', '==', 'value'], 'or', ['field', '==', 'value']]

With this syntax you can create most of the filter expressions.

Allowed operations: ==, !=, <, >, >=, <=, Contains, ContainsCaseInsensitive, StartsWith, StartsWithCaseInsensitive, EndsWith, EndsWithCaseInsensitive, InArray, ArrayContains, NotEqualIgnoreCase and EqualIgnoreCase

Complex query

This example shows how to combine different conditions using or/and. This allows you to create complex conditions.
In this example all values are queried that

start with test and end with V
or
start with var and end with 2;

\f:(typescript:Angular) export class DemoComponent implements OnInit {\n \tvalues$: Observable<Entry[]>;\n\n \tconstructor(private db: SapphireDbService) { }\n\n \tngOnInit() {\n \t\tthis.values$ = this.db.collection <Entry>('entries')\n \t\t\t.where([\n \t\t\t\t[['content', 'StartsWith', 'test'], 'and', ['content', 'EndsWith', 'V']],\n \t\t\t\t'or',\n \t\t\t\t[['content', 'StartsWith', 'var'], 'and', ['content', 'EndsWith', '2']]\n \t\t\t])\n \t\t\t.values();\n \t}\n }
[]

Example - Get value by id

null
\f:(typescript:Angular) export class DemoComponent implements OnInit {\n \t\tid$ = new ReplaySubject<string>();\n \t\tvalueById$: Observable<any>;\n\n \tconstructor(private db: SapphireDbService) { }\n\n \tngOnInit() {\n \t\tthis.valueById$ = this.id$.pipe(\n \t\t\tswitchMap((id: any) => {\n \t\t\t\treturn this.db.collection<any>('demo.entries').where(['id', '==', id]).first().values();\n \t\t\t})\n \t\t);\n \t}\n }
Continue with order prefilters

This website uses Google Analytics. If you want to disable it just unselect the option: