Correct way to check if subdoc exists?

Hello,

a little bit confused with docs here Sub-Document Operations | Couchbase Docs, it says that:

LookupInResult has an Exists method, but this should not be confused with the exists operation . The Exists method is used to check if anything was returned by the server for a given operation. The result of the exists operation should be checked with ContentAs<bool>(index) , as in the example.

Using CouchbaseNetClient 3.2.3, CouchbaseServer 7.0.2
Requesting subdoc like this:

await collection.LookupInAsync(id, attr => attr.Exists(“a property to check”))

the result of ContentAs<>(0) always False,
the result of Exists(0) returns correct value: True if property exists and False otherwise.

Is this intentional behaviour or a bug?
Thanks in advance!

@Cyb -

Yes, it is somewhat confusing, there are two different exists methods.

  • LookupInSpecBuilder.Exists - is a server operation that looks into the JSON document to determine if a field exists or not.

  • ILookupInResult.Exists - checks the result of the LookupInSpecBuilder.Exists method to see if the server found the field specified in LookupInSpecBuilder.Exists.

The difference between ILookupInResult.Exists and ILookupInResult.ContentAs<T> is that the Exists looks at the specs status (from the server) and ContentAs<T>() tries to fetch the actual value of the spec. I am pretty sure the server doesn’t actually return a bool here, just the status.

Jeff

@jmorris -

For me it was not confusing at all. Originally I implemented existence checking via Exists-Exists. I was looking documentation for entirely something else and stumbled upon the passage where said that the Exists operation result must be checked with ContentAs method. And that was confusing.

Thanks Jeff!

Your right, this is incorrect! I created an issue for tracking as now I am not sure if the docs are wrong or if its a bug in the sdk!