-
Hi, I just found out about this database and I'd really like to know more about it, but, I can't seem to figure out how to do some of (what I consider to be) simple things with it :). Right now I just wish to insert a document (explicitly specifying the ID) on the condition that there is no existing document with the same ID is already stored. However I can't figure out how :D. Are there any examples on how this is done? I first thought just calling session.StoreWithChangeVectorAndID combined with empty change vector would do the trick, but apparently that does something else :D (I can repeat the same operation and save the changes as many times as I like, and I just end up creating multiple revisions of the document) What is the right way of thinking to achieve this operation in a ravendb idiomatic way? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Hmm seems like I should be using the compare exchange functionality. I am able to use it in go, but it doesn't seem like the go client supports cluster wide transactions, so I cannot couple the compare exchange operation to my regular storing of a document, i.e. I will have a dangling compare exchange object if the regular storage fails :S |
Beta Was this translation helpful? Give feedback.
Hey @GiGurra - you can do this by enabling optimistic concurrency (
session.Advanced.UseOptimisticConcurrency
) on a single-node level.It can still result in dupes in some scenarios so you're right that the only way to absolutely guarantee it is a cluster wide transaction or a manual compare exchange.
I am not familiar with the Go client so maybe someone else will comment there, but you can still do it like you propose - it just won't be atomic.
Store the document with a normal ID, then if you succeed, create a compare exchange KV with the unique key as key and the document id as value. If it fails (whether it's not unique or a network error or whatever), you can retry it.