1

I have this data model.

/tests/testId

{
   "photos":{
      79075240-f6c3-11ea-9d76-c328c656dbfc:{
         "url":"",
         "votes":0
      },
      7a394290-f6c3-11ea-bd51-5d216a9dfad9:{
         "url":"urlperPhoto"
         "votes":0
      }
   },
   "moderated":false,
   "owner":o8SIEjIByyaNciEgCFH5Kfh4ngh2,
   "active":false,
   "votes":0
}

/tests/testId/votes

{
    photoId: 'xxx',
    birthday: null,
    sex: false,
    votedDate: null
}

I would like to get a list of posts without which I voted. Because I have voted in other collections so I can add additional field for the post model.

Example: votedUsers: [user1, user2, user3] or votedUsers: {user1: true, user2: true}

But... I don't have in firebase filter like "not exists". How can I display posts for the user, without this which he voted?

LUshosiu
  • 161
  • 1
  • 2
  • 13

1 Answers1

0

This sort of query is not possible with Firestore, as there are no indexes for data that doesn't exist. You can only query for data that does exist, and is indexed. This means that you will need to execute one query to get some possible items that the user has not voted on, then compare that to the results of another query that checks to see if that user has voted on, and remove those from the result set. Yes, this is difficult, and potentially expensive. But this is just not the sort of problem that Firestore is good at.

You might want to consider using another data along with Firestore in order to maintain this sort of relationship between users and things they have not yet seen or done. (It just won't scale like Firestore.)

See also:

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • Solution from your link probably is bad for my use case. I can check each post is in voted field/collection, but If I have limit in my query (example I would like to show 30 posts) What if each of them will be the one I have already voted for? – LUshosiu Sep 14 '20 at 20:32
  • Then query for 30 more using pagination. https://firebase.google.com/docs/firestore/query-data/query-cursors – Doug Stevenson Sep 14 '20 at 20:52
  • if I find posts without which I voted on 25 page (and if will be more votes 26,27, and more) I get useless data. I will lose user time and server time (and cost). – LUshosiu Sep 14 '20 at 21:11
  • As I said, "Yes, this is difficult, and potentially expensive". You don't have any viable alternatives unless you find another way to start finding possible items to vote on. – Doug Stevenson Sep 14 '20 at 21:15