Questions about the Couchbase client APIs
Wed, 12/19/2012 - 17:45
- To create a document, is there a single client API that tells us the object already exists without creating the object or the need of making a get() call prior to the actual create call? Neither the set() nor the add() method seem to give me that.
- To update an existing document, is the replace() method a good choice?
Thanks,
Connie
Wed, 12/19/2012 - 20:22
Hello,
1. To "create" a document you have two possible operations:
- set : will create if document does not exist, will update if the document already exists (no warning message)
- add : will fail if the document already exist
You can find information about these operations
http://www.couchbase.com/docs/couchbase-devguide-2.0/cb-store-operations...
2. To replace/update a document you have 2 options:
- set : see above
- update : will faill if the document does not exist
You can find more information about "updating" a document here, since you have multiple options:
http://www.couchbase.com/docs/couchbase-devguide-2.0/cb-update-operation...
Regards
Tug
1. Interesting question, traditionally/typically that's the purpose of the get(), get's are extremely fast, if the key doesn't exist, it is a nearly instant return since all keys(metadata) are kept in memory.
The set() method *always* sets, overwriting what's there without error, which isn't what you want in this case.
get() and add() can be used sequentially, but if you know you want to create, simply using add() works exactly how you want it to and you don't need the get() first because it will fail immediately (nearly instantly) if it exists.
2. Replace is definitely a good choice if logically the Document should already exist (i.e. a user updating their password). It raises an error if it doesn't exist already. It's the complement to add().
Your question prompted me to ask the SDK team to potentially add a method called check() which returns true/false if the key exists or not, but this is a very niche and rare need to not actually get the value, just existence only. If you are following it with an add() then you don't really need a check(), just handle the exception.
Hope this was helpful!
@scalabl3
Technical Evangelist
Couchbase Inc.