Search code examples
realmreact-native

Getting the highest id value in Realm JS


I need to get the highest id value that was inserted into Realm as Primary Key. I understand that using filters can give a list of values,

let hondas = realm.objects('Car').filtered('id');

So how do I get the highest id from the table, say at the 0th position?


Solution

  • Based on https://realm.io/docs/javascript/latest/api/Realm.Collection.html#sorted

    let hondas = realm.objects('Car').sorted('id', true);
    let highestId = hondas[0].id;