0
votes

i've been developing a mobile app with react native & firebase realtime db, and i'm stuck with a data-modeling problem.

My app will let users to vote photos that uploaded by other users and each user will be allowed to vote once for each photo. I'm providing a tinder-like UI for voting action. I'm planning to have users and photos trees on the firebase looks like this, which is pretty straight forward:

{
  users:{
    userId1: {
       name:'John'
       surname: 'Doe',
       votedPhotos: {
         somePhotoId: {
           timestamp: 1528836856000
         },
         somePhotoId2: {
           timestamp: 1529363754000
         },
         ...
       },
       ...
    },
    userId2: {
       name:'Johnny'
       surname: 'Doerr'
       ...
    },
    ...
  },
  photos: {
    photoId1: {
      url: 'https://a-firebase-storage-url',
      owner: {
        uid: 'userId1',
        fullName: 'John Doe'
      },
      upvoteCount: 12,
      downvoteCount: 8
    },
    photoId2: {
      url: 'https://another-firebase-storage-url',
      owner: {
        uid: 'userId2',
        fullName: 'Johnny Doerr'
      },
      upvoteCount: 28,
      downvoteCount: 4
    },
    ...    
  }
}

I need to prevent users to vote their own photos and vote a photo more than once. So i need to query photos as excluding already voted photos and self uploaded photos for a user. If i was using a traditional db it would be easy as a pie but i couldn't figure out how to do that query in a firebase realtime database since i can't use not_equals or not_in. How would you suggest me to model my data?

1
It sounds like from your question that filtering on the client is not acceptable? - Doug Stevenson
unfortunately it doesn't seem like a scalable solution for a scalable db. - DonkeyKong

1 Answers

0
votes

Record user votes under the photouid. I imagine a .length could provide you the total votes.

photos
  photo1-uid
    owner:user1-uid
    votes:{
      user2-uid: true,
      user3-uid: true
    }