From 76acf0ab60691a44945d092092043f35a72a3b2a Mon Sep 17 00:00:00 2001 From: astigsen Date: Tue, 27 Feb 2018 14:30:31 -0800 Subject: [PATCH 1/4] Updated query docs in API ref to reflect the features of the new query parser --- docs/tutorials/query-language.md | 58 ++++++++++++++++++++++++++++++-- 1 file changed, 55 insertions(+), 3 deletions(-) diff --git a/docs/tutorials/query-language.md b/docs/tutorials/query-language.md index a7a093e8..35e9aeb6 100644 --- a/docs/tutorials/query-language.md +++ b/docs/tutorials/query-language.md @@ -23,7 +23,7 @@ let merlots = wines.filtered('variety == $0 && vintage <= $1', 'Merlot', maxYear ``` -### Relational operators +### Conditional operators You can use equality comparison on all property types: `==` and `!=` @@ -35,7 +35,7 @@ Example: let oldContacts = realm.objects('Contact').filtered('age > 2'); ``` -Note that for boolean properties, you should test against the expected keyword. +Note that for boolean properties, you should test against `true` or `false`. Example: ```JS @@ -43,7 +43,7 @@ let women = realm.objects('Contact').filtered('isMale == false'); ``` ### String operators -For string properties, prefix, suffix, and substring queries are supported by using the `BEGINSWITH`, `ENDSWITH`, and `CONTAINS` operators. +For string properties, prefix, suffix, and substring queries are supported by using the `BEGINSWITH`, `ENDSWITH`, `CONTAINS` and `LIKE` operators. For any string operation you can append `[c]` to the operator to make it case insensitive. @@ -53,6 +53,58 @@ let peopleWhoseNameContainsA = realm.objects('Contact').filtered('name CONTAINS[ let Johns = realm.objects('Contact').filtered('name ==[c] "john"'); ``` +You can do simple wildcard matching with `LIKE` which supports using `?` to match a single character and `*` to match zero or multiple characters. + +Example: +```JS +// Matches "John" and "Johnny" +let Johns = realm.objects('Contact').filtered('name LIKE "John*"'); +``` + ### Composition Use parentheses and the `&&`/`AND` and `||`/`OR` operators to compose queries. You can negate a predicate with `!`/`NOT`. +### Queries on collections + +When objects contain lists you can query into them using the collection operators `ANY`, `ALL` and `NONE`. + +Example: +```JS +// Find contacts with one or more teenage friends +let teens = realm.objects('Contact').filtered('ANY friends.age < 14'); + +// Find contacts where all friends are older than 21 +let adults = realm.objects('Contact').filtered('ALL friends.age > 21'); +``` + +You can query on aggregates over properties in the lists using the aggregate operators `.@count`, `.@avg`, `.@min`, `.@max` and `.@sum`. + +Example: +```JS +// Find contacts without friends +let teens = realm.objects('Contact').filtered('friends.@count == 0'); + +// Find contacts where the average age of their friends is above 40 +let adults = realm.objects('Contact').filtered('friends.@avg.age > 40"'); +``` + +Subqueries using the `SUBQUERY` operator allows you to filter the lists across multiple parameters while querying them. + +Example: +```JS +// Find contacts with friends above 21 in SF +let teens = realm.objects('Contact').filtered('SUBQUERY(friends, $friend, $friend.age > 21 AND $friend.city = 'SF').@count > 0'); +``` + +### Backlink queries + +Other objects can link to an object and you can query on that releationship using the `@links` and `@links.ClassName.PropertyName` syntax: + +Example: +```JS +// Find contacts with no incomming links +let lonely = realm.objects('Contact').filtered('@links.@count == 0); + +// Find contacts where someone from SF has them as friends +realm.objects('Contact').filtered('@links.Contact.friends.city == "SF"); +``` From 02ba8183e137e12b0aad9bf5beda32d8acc244be Mon Sep 17 00:00:00 2001 From: astigsen Date: Tue, 27 Feb 2018 14:33:11 -0800 Subject: [PATCH 2/4] Fixed missing chars --- docs/tutorials/query-language.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/tutorials/query-language.md b/docs/tutorials/query-language.md index 35e9aeb6..f34702f2 100644 --- a/docs/tutorials/query-language.md +++ b/docs/tutorials/query-language.md @@ -103,8 +103,8 @@ Other objects can link to an object and you can query on that releationship usin Example: ```JS // Find contacts with no incomming links -let lonely = realm.objects('Contact').filtered('@links.@count == 0); +let lonely = realm.objects('Contact').filtered('@links.@count == 0'); // Find contacts where someone from SF has them as friends -realm.objects('Contact').filtered('@links.Contact.friends.city == "SF"); +realm.objects('Contact').filtered('@links.Contact.friends.city == "SF"'); ``` From d1df6d86f834b7125eeb43bd728fb3bf24c7dfc1 Mon Sep 17 00:00:00 2001 From: astigsen Date: Tue, 27 Feb 2018 16:37:45 -0800 Subject: [PATCH 3/4] Minor review fixes --- docs/tutorials/query-language.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/tutorials/query-language.md b/docs/tutorials/query-language.md index f34702f2..3677ede0 100644 --- a/docs/tutorials/query-language.md +++ b/docs/tutorials/query-language.md @@ -82,10 +82,10 @@ You can query on aggregates over properties in the lists using the aggregate ope Example: ```JS // Find contacts without friends -let teens = realm.objects('Contact').filtered('friends.@count == 0'); +let lonely = realm.objects('Contact').filtered('friends.@count == 0'); // Find contacts where the average age of their friends is above 40 -let adults = realm.objects('Contact').filtered('friends.@avg.age > 40"'); +let adults = realm.objects('Contact').filtered('friends.@avg.age > 40'); ``` Subqueries using the `SUBQUERY` operator allows you to filter the lists across multiple parameters while querying them. @@ -93,7 +93,7 @@ Subqueries using the `SUBQUERY` operator allows you to filter the lists across m Example: ```JS // Find contacts with friends above 21 in SF -let teens = realm.objects('Contact').filtered('SUBQUERY(friends, $friend, $friend.age > 21 AND $friend.city = 'SF').@count > 0'); +let teens = realm.objects('Contact').filtered('SUBQUERY(friends, $friend, $friend.age > 21 AND $friend.city = "SF").@count > 0'); ``` ### Backlink queries From 3433654a97a5c38f345391b0e362999ec9008919 Mon Sep 17 00:00:00 2001 From: astigsen Date: Tue, 6 Mar 2018 13:43:10 +0100 Subject: [PATCH 4/4] Removed backlink queries (for now) --- docs/tutorials/query-language.md | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/docs/tutorials/query-language.md b/docs/tutorials/query-language.md index 3677ede0..57cb0b9a 100644 --- a/docs/tutorials/query-language.md +++ b/docs/tutorials/query-language.md @@ -95,16 +95,3 @@ Example: // Find contacts with friends above 21 in SF let teens = realm.objects('Contact').filtered('SUBQUERY(friends, $friend, $friend.age > 21 AND $friend.city = "SF").@count > 0'); ``` - -### Backlink queries - -Other objects can link to an object and you can query on that releationship using the `@links` and `@links.ClassName.PropertyName` syntax: - -Example: -```JS -// Find contacts with no incomming links -let lonely = realm.objects('Contact').filtered('@links.@count == 0'); - -// Find contacts where someone from SF has them as friends -realm.objects('Contact').filtered('@links.Contact.friends.city == "SF"'); -```