1

Support I have a database like below:

{
  "eventAttendees" : {
    "fm" : {
      "9" : "Alice",
      "23" : "Khanh",
      "43" : "Bach"
    }
  },
  "events" : {
    "fm" : {
      "date" : "2017-06-16",
      "name" : "I love Firebase Meetup in Tokyo yesterday". 
    },
    "gm" : {
      "date" : "2017-08-12",
      "name" : "Meet Linh"
    }
  },
  "users" : {
    "1" : {
      "name" : "David"
    },
    "2" : {
      "name" : "Alice"
    },
    "10" : {
      "name" : "Khanh"
    }
  }
}

If I query the keyword "Firebase Meetup", it works.

let query = ref.child("events").queryOrdered(byChild: "name").queryEqual(toValue: "Firebase Meetup").queryLimited(toFirst: 1)
        query.observe(.value, with: { (snapshot) in
             let content = snapshot.value as? [String : AnyObject] ?? [:]
            print(content)

-> Return

["fm": {
    date = "2017-06-16";
    "name" : "I love Firebase Meetup in Tokyo yesterday". ;
}]

but if I query the keyword "Firebase", it does not work.

If it possible to query "Firebase" and get result like below?

 ["fm": {
            date = "2017-06-16";
            "name" : "I love Firebase Meetup in Tokyo yesterday". ;
        }]
KENdi
  • 7,576
  • 2
  • 16
  • 31
John
  • 3,888
  • 11
  • 46
  • 84

1 Answers1

0

you can use the startAt() function:

let query = ref.child("events").queryOrdered(byChild: "name").queryStart(at: "I love Firebase").queryLimited(toFirst: 1)
        query.observe(.value, with: { (snapshot) in
             let content = snapshot.value as? [String : AnyObject] ?? [:]
            print(content)

Documentation here https://firebase.google.com/docs/reference/js/firebase.database.Query#startAt

In addition, if you want start with Firebase or ending with Meetup, just do:

let query = ref.child("events").queryOrdered(byChild: "name").queryStart(at: "Firebase").queryEnd(at: "in Tokyo yesterday").queryLimited(toFirst: 1)
        query.observe(.value, with: { (snapshot) in
             let content = snapshot.value as? [String : AnyObject] ?? [:]
            print(content)

Documentation here https://firebase.google.com/docs/reference/js/firebase.database.Query#startAt

developer_hatch
  • 15,898
  • 3
  • 42
  • 75
  • startAt will work because "Firebase" is at the beginning of the string. What if I query "Meetup" ? – John Jun 15 '17 at 18:24
  • @John, in your question, that's is what you ask, or maybe I'm wrong? what are you trying to achieve? – developer_hatch Jun 15 '17 at 18:35
  • @John I updated the answer, let me know if that is what you expecting :) – developer_hatch Jun 15 '17 at 18:44
  • Hi. I am sorry for confusing question. I mean if the "name" : "I love Firebase Meetup in Tokyo yesterday". Can .queryStart(at: "Firebase").queryLimited(toFirst: 1) return the same result? – John Jun 15 '17 at 18:49
  • No no, you need to add in that case "I love Firebase" @John and that's all :) – developer_hatch Jun 15 '17 at 18:50
  • so firebase can not implement searching a random keyword in a string? WHERE FOO LIKE '%bar%' – John Jun 15 '17 at 19:02
  • It looks like you're trying to implement a wildcard search. That indeed is not a feature that you can easily implement on the Firebase Database. See https://stackoverflow.com/questions/42951076/firebase-like-search-on-string – Frank van Puffelen Jun 15 '17 at 19:26
  • Thanks you Frank. – John Jun 16 '17 at 00:28