I'm trying to store a simple hello world in c# in开发者_JAVA百科 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
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.
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
精彩评论