Filter children without parent?



I have a many-to-one relationship database structure, similar to the many-to-one Dog/Owner relationship cited in the documentation. I was wondering if I could filter by dogs without owner?

I would appreciate any help.


Hi Jay,

Here is my class Owner:

class Owner: Object {
    @objc dynamic var name: String = ""
    let dogs = List<Dog>()

And my class Dog:

class Dog: Object {
    @objc dynamic var name: String = ""
    @objc dynamic var breed: String = ""
    var owner = LinkingObjects(fromType: Owner.self, property: "dogs")

Owners can have multiple dogs… Dogs are only assigned one owner at most. Some dogs don’t have owners and I want to be able to display a list of those using the filter function. I tried something like this but it didn’t work:

 realm.objects(Dog.self).filter("%@ IS nil", owner)

Thank you.


Possibly - can you edit your question to include your Dog and Owner objects and explain what you are filtering for and what the expected result is? i.e. All Dogs age of 2 or All Dogs age of 2 owned by Fred?


I figured out how to check if a dog is not assigned an owner, so I’m halfway there I suppose:

dogs = realm.objects(Dog.self)
            for dog in dogs! {
                if item.owner.isEmpty {
                    print("Dog for adoption")

I need to now figure out a way to filter for dogs with empty “owner.”


You’ve got a number of options so let me throw out two.

Since the relationship from owners to dogs is one to many and the inverse is 1-1 (Dogs have 1 owner at most) just change your dog class to point to one owner

class DogClass: Object {
    @objc dynamic var name: String = ""
    @objc dynamic var breed: String = ""
    @objc dynamic var owner: Owner? = nil

With that you can query for nil on the owner property for all dogs that have no owners.

if let results = realm?.objects(DogClass.self).filter("owner == nil")  {

In your Dog class, the var LinkingObjects property is called ‘owner’ but it’s actually an array so should probably be ‘owners’. You can’t check for nil on that array property but you can easily use aggregate functions to see if the are any elements in the array. To check for all dogs with no owners, you can do this:

if let results = realm?.objects(DogClass.self).filter("[email protected] == 0")  {


Awesome! Thank you, Jay. That’s exactly what I was looking for.