Parse Server How to get an object ID by query?

3.1k Views Asked by At

If we wanna get an object ID we should do this:

String objectId = gameScore.getObjectId();

but what if we wanna get an object ID by a query? Like this:

ParseQuery<ParseObject> query = ParseQuery.getQuery("mytable");
query.whereEqualTo("Title", "Adrians Book");

List<ParseObject> results = null;

 try {

        results = query.find();

        if(!results.isEmpty()) {
        String objectId = results.getObjectId();
        }

    } catch (com.parse4cn1.ParseException e) {

        Dialog.show("Err", "Something went wrong.", "OK", null);
    }

Sounds interesting don't you think? I wish it could be possible. As you can see in this example the query will get a value from a specific object in the table which could track for the object ID then returning it as well. ParseQuery class should be implemented with getObjectId(). Because by this way applications always could have access to object IDs from the query even after applications get restarted so in the first example the gameScore which is actually an instance of ParseObject would lost reference to the Database after restarting. Getting object IDs by the query it would be able to program applications to get object IDs automatically without the need of doing it manually nor depending on instances of ParseObject.

2

There are 2 best solutions below

2
On BEST ANSWER

@Shai Almog: Thank you very much for taking your time to look at the ParseQuery documentation. I accidentally figured out how to get this done!

ParseQuery<ParseObject> query = ParseQuery.getQuery("mytable");
query.whereEqualTo("Title", "Adrians Book");

List<ParseObject> results = null;

 try {
        results = query.find();
        if(!results.isEmpty()) {
        String objectId = results.get(0).getObjectId();
        System.out.println(objectId);
        }

    } catch (com.parse4cn1.ParseException e) {

        Dialog.show("Err", "Something went wrong.", "OK", null);
    }

Yep, after adding the method .get(index) it allows you to access the method .getObjectId() since results is a list of a ParseObject, then the respective objectId of your query result will be printed in the console! I'm pretty glad it's working because I won't need to serialize each object for now which would be a pain.

Also if you wanna set an instance of ParseObject with an existing objectId in case you need to update something in your Database, you can use this example:

ParseObject po = ParseObject.create("mytable");
po.setObjectId(//YOUR DESIRED OBJECTID HERE, AS LONG AS IT EXISTS IN THE DATABASE);
0
On

As far as I know you need to get the whole object then query it's ID. I don't see a query id method here https://github.com/sidiabale/parse4cn1/blob/41fe491699e604fc6de46267479f47bc422d8978/src/com/parse4cn1/ParseQuery.java