如何使用 .NET 客户端在 Couchbase 中插入原始 json?
How to insert raw json in Couchbase with the .NET client?
我正在为 Couchbase 上的 CRUD 操作开发 REST API。
想法是将来自请求正文的 RAW Json 直接发送到后端。
Couchbase 客户端根本不应该进行序列化。
实际上,您可以用一个简单的字符串(无需反序列化)从商店取回文档。
据我所知,目前还不支持。
我错了吗?
您可以对 Couchbase 执行 CRUD 操作
示例 来自 Couchbase Documentation
以下示例从名为 beer-db 的数据库中检索标识符为 beer_#17_Cream_Ale 的文档。因为标识符包含数字符号 (#) 字符,所以标识符必须 URL 在 URI 中编码。
要求
GET /beer-db/beer_%2317_Cream_Ale HTTP/1.1
Host: localhost:59840
回应
HTTP/1.1 200 OK
Accept-Ranges: bytes
Cache-Control: must-revalidate
Content-Length: 259
Content-Type: application/json
Date: Thu, 12 Dec 2013 21:12:28 GMT
Etag: "1-431506b53aeac96a225e619cfa7bb569"
Server: CouchbaseLite 1.486
{
"category" : "North American Lager",
"brewery" : "Big Ridge Brewing",
"style" : "American-Style Lager",
"updated" : "2010-07-22 20:00:20",
"_id" : "beer_#17_Cream_Ale",
"_rev" : "1-431506b53aeac96a225e619cfa7bb569",
"name" : "#17 Cream Ale"
}
Couchbase 会将有效的原始 JSON 字符串视为文档,因此您只需将字符串作为值发送并以相同的方式取回即可。这是最新的 .NET SDK(目前为 2.1.1)的示例:
string json = "{\"a\":\"b\"}";
bucket.Upsert<string>("my_id", json);
var op = bucket.Get<string>("my_id");
var str = op.Value;
Debug.Assert(string.Equals(json, str));
查看 Couchbase UI,我们可以看到我们的原始 JSON 字符串是一个正确的文档:
我正在为 Couchbase 上的 CRUD 操作开发 REST API。
想法是将来自请求正文的 RAW Json 直接发送到后端。
Couchbase 客户端根本不应该进行序列化。 实际上,您可以用一个简单的字符串(无需反序列化)从商店取回文档。
据我所知,目前还不支持。
我错了吗?
您可以对 Couchbase 执行 CRUD 操作
示例 来自 Couchbase Documentation
以下示例从名为 beer-db 的数据库中检索标识符为 beer_#17_Cream_Ale 的文档。因为标识符包含数字符号 (#) 字符,所以标识符必须 URL 在 URI 中编码。
要求
GET /beer-db/beer_%2317_Cream_Ale HTTP/1.1
Host: localhost:59840
回应
HTTP/1.1 200 OK
Accept-Ranges: bytes
Cache-Control: must-revalidate
Content-Length: 259
Content-Type: application/json
Date: Thu, 12 Dec 2013 21:12:28 GMT
Etag: "1-431506b53aeac96a225e619cfa7bb569"
Server: CouchbaseLite 1.486
{
"category" : "North American Lager",
"brewery" : "Big Ridge Brewing",
"style" : "American-Style Lager",
"updated" : "2010-07-22 20:00:20",
"_id" : "beer_#17_Cream_Ale",
"_rev" : "1-431506b53aeac96a225e619cfa7bb569",
"name" : "#17 Cream Ale"
}
Couchbase 会将有效的原始 JSON 字符串视为文档,因此您只需将字符串作为值发送并以相同的方式取回即可。这是最新的 .NET SDK(目前为 2.1.1)的示例:
string json = "{\"a\":\"b\"}";
bucket.Upsert<string>("my_id", json);
var op = bucket.Get<string>("my_id");
var str = op.Value;
Debug.Assert(string.Equals(json, str));
查看 Couchbase UI,我们可以看到我们的原始 JSON 字符串是一个正确的文档: