Query -> Check if object exists in db

Hey guys,

What’s the most performant way to check if an object exists in the database without fetching it’s data?

Currently I’m excluding all fields and using query.first().
However if I add a new field to that class I’ll have to update my code to exclude that new field too.

  1. Is there something like a query.exists()?
  2. If I want to .select(**NOTHING**), what’s the correct way to do it? (even thought this downloads some unnecessary data like _id and _created_at at least it would be easier to maintain.

Thanks in Advance.

Have you tried query.select('objectId');query.first();?

1 Like

Hey @davimacedo, sorry for taking so long to reply!

select("objectId") has the same effect as select("") therefore I’ll just use the 2nd approach.

However if there was a way to not get the createdAt/updatedAt/objectId data and just get a boolean result to check if the entry exists that could be useful to not waste any bandwidth.

Thanks for replying as always

You could do something such as:

Parse.Query.prototype.doesExist = async function(args) {
  return !!await this.select("").first(args)
}

And then:

const exists = await new Parse.Query(Parse.User).equalTo(...).doesExist();
// exists is a bool