1

I'm trying to store a simple hello world in c# in a couchdb Database (total newbie).

I'm using LoveSeat (but feel free to suggest another client if it would help) and have created this simple code:

static void Main(string[] args)
{
    var cl = new CouchClient("myserver", 5984, null, null);

    var db = cl.GetDatabase("mydb");

    var newDoc = db.CreateDocument(@"{
""Test"":""ValueTest""
}"
    );

    var newDoc2 = db.SaveDocument(newDoc);
}

The document is actually created :

{
   "_id": "805656b6113d30a5387230a669000bb6",
   "_rev": "1-44c5768a2fa004c6a43899687c283517",
   "Test": "ValueTest"
}

but when I look at the resulting newDoc2, I see :

{
  "error": "file_exists",
  "reason": "The database could not be created, the file already exists."
}

Did I do something wrong ?

thx

Matt Passell
  • 4,549
  • 3
  • 24
  • 39
Steve B
  • 36,818
  • 21
  • 101
  • 174

2 Answers2

1

CreateDocument is what actually adds the new document to couchdb. There is no need to call SaveDocument after that. SaveDocument is used to update a document after you make changes to it. I'm not sure, but it seems that calling SaveDocument on a document that has not been updated gives you this error. If you make a change to the document and then call SaveDocument, I expect it will work fine.

user642118
  • 26
  • 2
  • from the couchdb wiki: To update an existing document, you also issue a PUT request. In this case, the JSON body must contain a _rev property, which lets CouchDB know which revision the edits are based on. If the revision of the document currently stored in the database doesn't match, then a 409 conflict error is returned. – Peter Long Jun 22 '11 at 09:49
1

I read the source code of LoveSeat:

public Document SaveDocument(Document document)
{
    if (document.Rev == null)
        return CreateDocument(document);

    var resp = GetRequest(databaseBaseUri + "/" + document.Id + "?rev=" + document.Rev).Put().Form().Data(document).GetResponse();
    var jobj = resp.GetJObject();
    //TODO: Change this so it simply alters the revision on the document past in so that there isn't an additional request.
    return GetDocument(document.Id);
}

so if document.Rev==null, SaveDocument is the same as CreatDocument
if document.Rev!=null, SaveDocument is actually "UpdateDocument".

Your code belongs to the former case: document.Rev==null

Peter Long
  • 3,964
  • 2
  • 22
  • 18