2

If I have the following structure in Firebase:

{
  "images": {
    "first_image": {
      "url": "https://ima.ge/first_image",
      "uploader": "john",
      "timestamp": "1465920841"
    },
    "second_image": {
      "url": "https://ima.ge/second_image",
      "uploader": "louis",
      "timestamp": "1465920987"
    },
    "third_image": {
      "url": "https://ima.ge/third_image",
      "uploader": "peter",
      "timestamp": "1465920990"
    }
  },

  "votes": {
    "first_image": {
      "john": "up",
      "louis": "down"
      "francis": "up"
    },
    "second_image": {
      "john": "up",
      "louis": "down"
    },
    "third_image": {
      "francis": "up"
    }
  }
}

Is it possible to query for example all the images which john has not voted for already with Firebase?

I read through the documentation and as I understand it, these kinds of queries are not possible at all. So if this is not possible, how could I try to re-structure my data in order to be able to make queries like that in my given example?

I am using the "new" Firebase console and currently the iOS and Android version of the Firebase SDK but I don't need a specific example, instead I want to understand how it is possible to make complex queries in Firebase, either by re-structuring my data or, if I misunderstood the documentation, through normal queries.

The biggest problem of filtering data on the client side is that there could potentially be millions of images and retrieving all of them would be really bad...

Ybrin
  • 901
  • 8
  • 30

1 Answers1

4

Yes, You can do this.

Here's an example to find nodes that do NOT have john voting, which will return the third_image node from the structure in the question.

let ref = self.myRootRef.childByAppendingPath("votes")
ref.queryOrderedByChild("john").queryEqualToValue(nil)
      .observeEventType(.Value, withBlock: { snapshot in
      for child in snapshot.children {
           print(child)
      }
})

Remember .Value returns multiple nodes so we need to iterate over then with for child... to get to each one.

As a side note, if these are Firebase users, you should use the userId instead of the persons name. Disassociate dynamic data (a persons name for example) from it's key whenever possible and refer to the key. John may want to be called Johnny in the future.

Jay
  • 34,438
  • 18
  • 52
  • 81
  • Thank you, actually I am using UIDs for users as well as images. This was just a simple example to not confuse anybody with uids. – Ybrin Jun 14 '16 at 17:48
  • Another question which may be related: If I have a "start" and an "end" value which are basically timestamps, how would it be possible to get only those childs which are "running"? So where the current timestamp is within these two timestamps. Is this even possible because I thought multiple queries are not possible. Could I structure it such that this is possible? – Ybrin Jun 16 '16 at 09:01
  • @Ybrin Not so I understand the question. Are you looking for nodes have values between two other values? Either way, this sounds like a new question so post it and let's see what we can come up with. – Jay Jun 16 '16 at 18:17
  • Ok so this is the new question: http://stackoverflow.com/questions/37867091/query-value-between-two-other-values-in-firebase – Ybrin Jun 16 '16 at 18:45