Get Data that is not related

Hello!

I have some problems with receiving Data that has no relation to the current User.
I created a Social Media App for personal training and I wanted to have a section where the user gets recommended other users which he is not following yet. In my Database, I have for each user two columns, one for following and one for followers.
Some of the solutions I tried as where Clause to get the users were:

`objectId not in (Users[objectId='${currentUserId}'].following.objectId)`

This basically works, but returns an empty array if the user is not following anybody yet

`(not(followers in ('${currentUserId}')) or followers is null)`

this does not work, it always returns all users

`followers.objectId != '${currentUserId}'`

This does not work, it returns all users as well. With “=” it works but somehow with “!=” not

`not('${currentUserId}' in (followers))`
//or
`'${currentUserId}' not in (followers)`

this returns all users that have some followers, so those who don’t have an empty followers relation list

I am not sure which Backendless Version I have but I guess it’s the newest, I use the online version on the Springboard Plan
I work with the JavaScript SDK
My Application ID is: AD410151-2D10-4D0D-FFE5-2F082E483500

I hope this is enough information to understand the Problem and I hope somebody can help me.

Already thanks in advance

Kind regards
Lucas

Hello @Lucas_Kiers

You want to retrieve users which you do not follow, but who follow you. Correct?

not quite, I just want users that I do not follow, who they follow does not matter

We could make it in two steps.

  1. Create a list of all users which you follow
  2. Create a whereClause from that list

objectId not in ('<user_objectId_you_follow_1>', '<user_objectId_you_follow_2>', ...)

Regards, Dima

Isn’t that basically what I did in my example 1? And there was the problem, that if I was following nobody, then nothing was returned.

And if I would call the whole following list from the user first and then map it in the whereClause of the next request, wouldn’t that be very time-consuming, especially if the users follows 100+ users?

Isn’t there an easier solution, where you can just get every user where currentUserId is not in related column, in this case followers?

Hi Lucas,

I believe the following query is the best version of it:

objectId not in (Users[objectId='${currentUserId}'].following.objectId)

However, I’d also modify it as follows because otherwise, the user gets himself.

objectId not in (Users[objectId='${currentUserId}'].following.objectId) and following.objectId != '${currentUserId}'

You’re right that the problem is that when the following relation is empty, the result is an empty collection. This happens because the following subquery fails internally as it cannot derefence the .objectId part:

objectId='${currentUserId}'].following.objectId

A hackish workaround is to have a dummy user that every other user follows, in other words, there will always be at least one record in the following relation.

Regards,
Mark

Hey Mark,

thanks for your response, I will try to implement this solution and hope to get it to work!
Thanks for your and Dimas help

Regards,
Lucas

Please let us know either way. I’d love to know if that approach works. I know this very question did come up in the past.

Regards,
Mark

I solved it now by first requesting the first user the current User is following, and if that is not null, then I add the objectId not in (Users[... part to the where Clause, if it is null I just let it at objectId != '${currentUserId}'

But I had a similar problem at another part. I have implemented chatrooms in the site and I added users as members of the chatroom as a one-to-many relation in the chatroom table. So the chatroom has a relation to all users, not the users to all chatrooms they joined.
And there I wanted to also be able to get the chatrooms the current Users hasn’t joined yet, but the most logical approach in my opinion, to use members != '${currentUserId}' did not work. And the other solution you suggested won’t work either, I think, because the chatroom manages the members and not the user the chatrooms they joined, If that is understandable.
But I scraped that Idea and left It out now, so that’s solved now as well. :sweat_smile:

Regards,
Lucas

1 Like