MyCouch (CouchDB) Visual Studio 基础知识(文档删除和 JSON 序列化)

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

我正在尝试使用 Visual Studio 和 CouchDB 构建通信协议,但是,我在一些非常基本的概念上遇到了问题,而且 myCouch git 还远未明确。 我想知道如何删除给定数据库名称的文档以及如何将从服务器接收到的 JSON 转换为可用代码。

到目前为止,这是我的 C# 代码

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();
        }
    }
}

来自 CouchDB 文档:

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.

这是一种避免覆盖其他人更新的方法。

因此,要删除文档,您首先需要加载它:

var myDoc = await store.GetByIdAsync(docId);

那你用文档Rev 属性删除它:

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

因此,除了 Id [=42= 之外,您的文档模型还必须具有 Rev 属性 ].

According the documentation 您的模型的 Rev 属性 可以命名为:

  • [实体名称]版本
  • 文档修订
  • 实体版本
  • 版本