Skip to content

Instantly share code, notes, and snippets.

@Drozerah
Created February 13, 2019 20:47
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save Drozerah/d74771132a294bf09494c2b406300152 to your computer and use it in GitHub Desktop.
Save Drozerah/d74771132a294bf09494c2b406300152 to your computer and use it in GitHub Desktop.
Firebase Realtime Database API - Query Exemples

Firebase Realtime Database API - Query Exemples

Documentation : firebase.database.Reference

── Query Methods ── firebase.database.Query

  orderByChild    (key)       Order children at location by the value of specified grandchild key.
  orderByKey      ()          Order children at location by child keys.
  orderByValue    ()          Order children at location by child values.

  limitToFirst    (num)       Max number of children to return from the beginning.  (Based on orderBy*.)
  limitToLast     (num)       Max number of children to return from the end.        (Based on orderBy*.)
  startAt         (val, key)  Return children who's val (and key, optionally) are >= the specified val (and key, optionally).
  endAt           (val, key)  Return children who's val (and key, optionally) are <= the specified val (and key, optionally).
  equalTo         (val)       Return children who's val is == the specified val.

── Exemples ──

import * as firebase from 'firebase'

{
  "users": {
     "1": {
        "name": "David",
        "email": "david@email.com",
        "age": "38",
        "location": "SF",
        "age_location": "99_SF",
     },
     "9": {
        "name": "Alice",
        "email": "alice@email.com",
        "age": "28",
        "location": "Paris",
        "age_location": "28_Paris",
     }
  }
}

// Get a reference to the root of the Database
const rootRef = firebase.database().ref
  // > The key of a root reference is null
  const key = rootRef.key;  // key === null

// 1. Select a user by UID
const oneRef = rootRef.child('users').child('1')

// 2. Find a user by email address
const twoRef = rootRef.child('users').orderByChild('email').equalTo('alice@email.com')

// 3. Limit to 10 users
const treeRef = rootRef.child('users').limitToFirst(10)

// 4. Get all users names that start with 'd'
const fourRef = rootRef.child('users').orderByChild('name').startAt('D').endAt('D\uf8ff')

// 5. Get all users ages who are less than 50
const fiveRef = rootRef.child('users').orderByChild('age').endAt('49')

// 6. Get all users who are greater than 50
const sixRef = rootRef.child('users').orderByChild('age').startAt('51')

// 7. Get all users ages who are between 20 and 100
const seveRef = rootRef.child('users').orderByChild('age').startAt(20).endAt(100)

// 8. Get all users who are 28 and live in Paris
const eightRef = rootRef.child('users').orderByChild('age_location').equalTo('28_Paris')
@NizarETH
Copy link

NizarETH commented Jun 3, 2020

// 8. Get all users who are 28 and live in Paris
const eightRef = rootRef.child('users').orderByChild('age_location').equalTo('28_Paris')

is not working for me :s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment