Upsert on Sync Gateway

Hi,

I was wondering if its possible to Upsert a document using the Sync Gateway?

I have spent a day trying to directly access the couchdb behind the sync gateway, and in the end it seems that I should only be added documents via the Sync Gateway, however it doesn’t have a upsert.

Is it possible to do? or do I have to do a get and then a put?

Regards

Mark

@mark.ellul,

You can insert data into sync gateway via REST.

if you have sync gateway running on your local machine. The Sync Gateway Admin GUI is at:
@http://localhost:4985/_admin/

The data port is the same as above but on different port: @http://localhost:4984/{db-name}

Here is the document for all the Methods for CRUD on the data port.
https://developer.couchbase.com/documentation/mobile/current/references/sync-gateway/rest-api/index.html?language=ios

Hey Mark,

In short, no, and the concept of an Upsert isn’t really a thing for Couchbase Mobile (but, of course Insert and Replace are things you can do), but there are reasons why. Point of order first: CouchDB != Couchbase Server; I assume you meant Couchbase Server which is what sits behind Sync Gateway.


in the end it seems that I should only be added documents via the Sync Gateway,

Yeah, for mutations, it’s pretty much the golden rule that you don’t change the documents directly in Couchbase Server. As you’ve probably noticed, Sync Gateway doesn’t just store the document, but also metadata under _sync and different documents all together.

In theory, it would make sense to allow an Upsert in a completely synchronous world, but the idea behind Couchbase Mobile is that different devices can be offline and change the same doc in different ways - hence MVCC. While there are use cases where it might make sense to have a “take this version as the absolute winning copy of this document, yes I’m absolutely sure”, imagine a case where two devices separately use that method - which of those two absolute winners should win?

Hey JFlath,

Thanks for your response.

CouchDB != Couchbase Server; I assume you meant Couchbase Server which is what sits behind Sync Gateway.

Yes we are using couchbase server.

While there are use cases where it might make sense to have a “take this version as the absolute winning copy of this document, yes I’m absolutely sure”, imagine a case where two devices separately use that method - which of those two absolute winners should win?

I understand that there are many use cases where the revisions are quite useful to ensure only the latest updates are accepted, however I thought that couchdb and couchbase server support an upsert feature, and hence Sync Gateway would as well.

In the use case that I am using the Sync Gateway, only a server will be writing over the same documents and any changes coming from mobile will be created as changelogs/event documents that the server will then process those events and if need be update the initial document.

For example, A document which describes a “meetup” thats going to occur, when one of the mobile client user clicks on “I’m going”, the mobile client is going to create a new document to record that event. If that mobile client is online, that document will be synchronized to the database and a server process will take that event and update the amount of people going to the “meetup”. If that mobile client is offline, when they come online their event documents will be pushed to the server.

Years ago, before couchdb had upsert, I had used it on a project, but the revision conflicts caused so many issues that I had to stop using couchdb for mongodb. I am hoping that this will not repeat itself again.

Do you know if upsert on the sync gateway is on the roadmap?

Thanks Househippo

I was asking about the ability to upsert a document, i.e. write a document to the server ignoring the revisions, and creating a new revision.

Regards

Mark