OData REST API — Small Useful Tricks (part 2)

Continuing a series of posts about the features of using OData protocol (see previous post).

3. How to Filter the Objects Based on Their Relationships

Suppose we want to get a list of people who like the book titled (property ‘title’) ‘book36’ (such as a funny book title we have in the example).

To get all the people we can write

…/persons

It is easy to filter objects by properties. For example, to filter persons by age (a person has the property ‘age’):

…/persons?$filter=age gt 30 – all the people at the age > 30 years

But how do we filter the people by a field of another object, for example, the title of the book the man likes?

Very simple: a person has the property ‘likes’ referring to the book, and a book has the property ‘title’. So we can write:

…/persons?$filter=likes/title eq ‘book36’

If you are interested in this post, you can also look through our documentation, REST API usage examples, and examples of using JavaScript library.

In the next post we will tell you about adding complex objects along with the related objects, and about adding arrays of objects.

Leave a comment