0

I have a database with documents like below:

Event
    0e354b76-aa36-4af1-aa38-ef22423c9968
        candy: 20
        date: "2019-04-22T17:00:30.234Z"
        id: "0e354b76-aa36-4af1-aa38-ef22423c9968"
        packages: []
        timestamp: 1555952430
        title: "Test edit"
        userId: "123"
    1572a408-9be5-4d00-9584-69492ddbccc1
        candy: 400
        date: "2019-05-02T03:04:05Z"
        id: "1572a408-9be5-4d00-9584-69492ddbccc1"
        packages: []
        timestamp: 1556766245
        title: "Another test"
        userId: "123"

I'd like to query for events that are in the future and have the userID 123. I'm using this golang FireBase library and I've figured out how to do each of these things individually:

ref := fbDB.NewRef("/Event")
// user id
ref.OrderByChild("user").EqualTo(uid).Get(c, &events);
// future events
ref.OrderByChild("timestamp").StartAt(time.Now().Unix()).LimitToFirst(numberToRetrieve).Get(c, &events);

but I can't figure out how to combine the two. How would I go about combining these two filters?

Updated structure

Event
   by_user
      123
        0e354b76-aa36-4af1-aa38-ef22423c9968
            candy: 20
            date: "2019-04-22T17:00:30.234Z"
            id: "0e354b76-aa36-4af1-aa38-ef22423c9968"
            packages: []
            timestamp: 1555952430
            title: "Test edit"
            userId: "123"
        0e354b76-aa36-4af1-aa38-ef22423c9968
            ...
Abe Miessler
  • 82,532
  • 99
  • 305
  • 486
  • Firebase Realtime Database doesn't support multiple filters. Instead, you can model your data differently as described in the dup. – Doug Stevenson Apr 25 '19 at 12:42
  • I'll encourage you to look into Cloud Firestore instead, as it overcomes many of the limitations of Realtime Database. – Doug Stevenson Apr 25 '19 at 15:25
  • @DougStevenson - sorry for sassing you. At this point I can't switch database platforms - maybe down the road so I'll look into Firestore. In the mean time, I looked at the question you referenced. If I restructure my code like I did in my question update wouldn't I still have the same problem? – Abe Miessler Apr 25 '19 at 15:38
  • Since your updated structure now groups events by user, you only need one filter to sold children by date for only that user, as the user ID is now implied by the path of the reference you'll use to query. – Doug Stevenson Apr 25 '19 at 15:51

0 Answers0