-1

So I had an object like this coming from firebase realtime database

  "3e97a0918fc5429ca470e017a9ff0b04": {
    "bearing": 0,
    "latitude": "19.9737038",
    "longitude": "73.7897944",
    "timestamp": "1557027317083",
    "trip": "3e97a0918fc5429ca470e017a9ff0b04",
    "user": "1346"
  },
  "43efa340444d454cb2673b9afd95c810": {
    "bearing": 329,
    "latitude": "20.1597784",
    "longitude": "73.8792233",
    "timestamp": "1554784486880",
    "trip": "43efa340444d454cb2673b9afd95c810",
    "user": "1339"
  },
  "4d1118279bd14597a59feaf883c1a1a4": {
    "bearing": 227,
    "latitude": "20.1588567",
    "longitude": "73.8851554",
    "timestamp": "1554681994097",
    "trip": "4d1118279bd14597a59feaf883c1a1a4",
    "user": "1467"
  },
  "4e28d129df8d4e399e094fd85f9891a0": {
    "bearing": 0,
    "latitude": "20.1450097",
    "longitude": "73.8935804",
    "timestamp": "1554940195655",
    "trip": "4e28d129df8d4e399e094fd85f9891a0",
    "user": "1488"
  },
  "584427ae15f240c896c13058e41a69b5": {
    "bearing": 177,
    "latitude": "20.1595356",
    "longitude": "73.8792819",
    "timestamp": "1554521339710",
    "trip": "584427ae15f240c896c13058e41a69b5",
    "user": "1316"
  },
  "5a4c13ed54584541b5da1fe6dc4d4405": {
    "bearing": 0,
    "latitude": "20.1597879",
    "longitude": "73.8792057",
    "timestamp": "1554523562777",
    "trip": "5a4c13ed54584541b5da1fe6dc4d4405",
    "user": "1511"
  },
}

But I want to only track trips with particular IDS. My Question is how do I limit the nodes by providing particular ids?

Let say I only wanted trips with ids

3e97a0918fc5429ca470e017a9ff0b04 43efa340444d454cb2673b9afd95c810

I tried using the equalTo method but that returns a single trip. And i also tried using orderByChild("trip") but that doesnt help either since the trips are uuids.

Malik Bagwala
  • 2,695
  • 7
  • 23
  • 38
  • What do you mean by "I want to only track"? Do you mean setting a listener with the `on()` method? https://firebase.google.com/docs/database/web/read-and-write#listen_for_value_events – Renaud Tarnec Jan 14 '20 at 07:59

2 Answers2

0

You cannot use and query in firebase database. If you want to retrieve data according to the id then you can use the equalTo() method that will retrieve according to one single id. If you want multiple ids, then you will have to create an attribute that will contain the ids that you want to query on. For example:

  "584427ae15f240c896c13058e41a69b5": {
    "bearing": 177,
    "latitude": "20.1595356",
    "longitude": "73.8792819",
    "timestamp": "1554521339710",
    "trip": "584427ae15f240c896c13058e41a69b5",
    "user": "1316",
    "ids": "584427ae15f240c896c13058e41a69b5_3e97a0918fc5429ca470e017a9ff0b04"
  },

Then you can use equalTo() on the child ids.

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
0

You write in your question that you want "to track trips". If you want to declare a listener for only a certain list of database nodes (i.e. trips) in order to listen to their changes (i.e. "tracking" them), you will need to call the on() method for each node.

Something like the following would do the trick:

  var database = firebase.database();
  var idsToListenTo = ["3e97a0918fc5429ca470e017a9ff0b04", "3e97a0918fc5429ca470e017a9ff0b04"];

  idsToListenTo.forEach(id => {
        database.ref(id).on('value', snapshot => {
            console.log("trip " + snapshot.key + " has just changed");
            console.log(snapshot.val());
        });
    }
  );
Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121
  • I knew this. but i wanted to do it via a single listener. I guess there isnt a better way.. Thanks for responding though.. highly appreciated :) – Malik Bagwala Jan 14 '20 at 13:43
  • Note that you could maybe consider the `child_changed` event type for the `on()` method, but it will be triggered for any child of a reference, therefore you would need to detect if the child key is part of the list of nodes you want to listen to (and you would generate unnecessary reads in case the key is not in the nodes list). – Renaud Tarnec Jan 14 '20 at 13:51