How to check document exists or not?

I have been looking into the PHP SDK 2 API and google around the web, but all I found is some outdated example… Such as How to check whether a document exists or not?

Currently if I use get() and key doesn’t exists, it will throw an CouchbaseException which said its not exists
with error code 13.
Handling an exception to see if the document exists seems heavy operation (generate stacktrace etc).
What is the proper way to check if the document exists currently?

So far, it seems the only way to check IS to invoke a get operation on the document and catch the exception, thrown, if there was one.

1 Like

That’s sad…

have you tried to use a count if returns 0 then there is no document …

Simon from our SDK team replied to a similar question with this:

How to check whether a document exists or not?

Basically, on the @Erutan409 is correct: if all you want to know is if the document exists, then you need to get it and handle the response accordingly. If you want to make sure you don’t overwrite an existing document with that key, then you can use ADD or REPLACE to only overwrite an existing document.

On the Couchbase Server side, at least, the operation is lightweight.

1 Like

@couchbwiss What do you mean by count? Do you mean by create a view and filter out the documents?

@mattew If that’s lightweight, then I am all fine :smile: