Hello @Dmitry_Kalenkov
Unfortunately, there is no simple way to do( or to fix/add) the case you have described. If we use direct MySQL I would use join with criteria, like the following:
select * from Article as a
left join ReadedArticles as ra on a.id=ra.artcileId and ra.userId=<my-userId>
So if ra
is null the user does not read the article, not null then it was read.
And 1:n relation in Backendless is actually is n:n relation, like I have described in MySQL example, so we have the intermediate table for relations like ReadedArticles
, but there is no way to provide additional join criteria. Without it, you will get cartesian intersection of the records for Articles and Users and you will not be able filter it in correct way.
Lets back to Backendless way. I have created the following schema
Now when I find the Article I can see the following
You can easily find the articles which were read by the user with the following query:
alreadyRead.objectId = <your-user-id>
But if you want to find Articles that was not read by the user, the following query will give you wrong result:
alreadyRead.objectId != <user-id>
as you can see you get both, but should get only one, because blog one was read by the user. And it is not a bug of Backendless it is just how n:n relation works. And the behavior might be improved if we add join criteria, but it is really long story.
As workaround to get articles that are not read by the user you can get Articles ids with wrist query:
➜ ~ curl -X GET -H 'user-token: 6B30E7AB-F531-4059-9C49-1746FF8B333A' 'https://api.backendless.com/23298D6F-EA2F-7BE6-FFBF-7507875E1E00/1C2B5588-DDCA-418F-A202-185385180687/data/Article?where=alreadyRead.objectId%20%3D%20%27FB5463F2-C33C-4794-B70F-8A46AC0D76D4%27&property=alreadyRead.objectId%20as%20readedBy&property=text&property=objectId' -i
HTTP/1.1 200 OK
server: nginx
date: Wed, 15 Mar 2023 10:05:41 GMT
content-type: application/json
content-length: 145
access-control-allow-origin: *
access-control-allow-methods: POST, GET, OPTIONS, PUT, DELETE, PATCH
strict-transport-security: max-age=86400
[{"___class":"Article","text":"Blog post 1","readedBy":"FB5463F2-C33C-4794-B70F-8A46AC0D76D4","objectId":"3C47539D-8761-44BE-8717-19B9566B476F"}]%
And the select articles that has no ids from first query:
objectId NOT in ('<objectId-1>', '<objectId-2>', '<objectId-3>',...)
Sorry for the long read