How to perform Realm count query
10,368
Solution 1
Yes, Realm still does not support .@count
query. You can work around that you modify the Person
model to have a count
property. Then you update the count
property when you append a dog object to the dogs
array.
This feature request is tracked by Issue #1166 https://github.com/realm/realm-cocoa/issues/1166
Update
Realm supports @count
and other collection queries from v0.96.
Solution 2
The syntax for the new aggregate expressions (@count
, @min
, @max
, @sum
, @avg
) for Results
and List
looks like this:
realm.objects(Person.self).filter("dogs.@count > 0")
Author by
Mario
Updated on July 25, 2022Comments
-
Mario almost 2 years
How can I do a count query on Realm?
for example this is my model
class Dog: Object { dynamic var name = "" } class Person: Object { dynamic var name = "" let dogs = List<Dog>() }
I want to fetch all persons with at least one dog something like
Realm().objects(Person).filter("dogs.@count > 0")
but @count isn't supported as i understand
-
uɥƃnɐʌuop over 8 yearsUpdate: it's now supported! Exciting! Your query
filter("dogs.@count > 0")
will work from version 0.96.0.
-
-
clozach about 8 yearsJust to save myself clicking through next time…the issue's closed.
@count
is now supported.