简体   繁体   中英

MyCouch (CouchDB) Visual Studio Basics (Document Deletion and JSON Serialization)

I am trying to build a communication protocol using Visual Studio and CouchDB however, I am having issues with some pretty basic concepts and The myCouch git is far from clear. I would like to know how to Delete a document given a database name and how to convert JSON received from the server into usable code.

Here is my C# code so far

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication3
{
    class Program
    {
        static async Task MainAsync()
        {
            using (var db = new MyCouch.MyCouchStore("http://UserName:Password@127.0.0.1:5984/", "patientsim"))
            {
                var json = await db.GetByIdAsync("1");
                Console.Write(json);
            }
        }
        static void Main(string[] args)
        {
            MainAsync().Wait();
        }
    }
}

From the CouchDB documentation:

If you want to change a document in CouchDB, you don't tell it to go and find a field in a specific document and insert a new value. Instead, you load the full document out of CouchDB, make your changes in the JSON structure (or object, when you are doing actual programming), and save the entire new revision (or version) of that document back into CouchDB. Each revision is identified by a new _rev value.

If you want to update or delete a document, CouchDB expects you to include the _rev field of the revision you wish to change.

These is a way to avoid overwriting somebody else updates.

So to delete a document you first need to load it with:

var myDoc = await store.GetByIdAsync(docId);

Then you use the document Rev property to delete it:

var deleted = await store.DeleteAsync(docId, myDoc.Rev);

So it is necessary that your document model has a Rev property in addition to the Id property.

According the documentation the Rev property of your models could be named:

  • [EntityName]Rev
  • DocumentRev
  • EntityRev
  • Rev

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM