我使用带有沙发的PHP和CouchDB来发布,放置,获取和删除数据。发布,获取,删除工作正常,但放置时出现问题。当我发送只用更新的属性值更新的放置请求文档,而所有其他属性在文档中删除时。
我已经发布,删除和获取数据
$response = $client->post(
"/child_installed_apps/_find",
[GuzzleHttp\RequestOptions::JSON => ['selector' =>
['parent_id' => ['$eq' => $userid], 'child_device_id' => ['$eq' =>
$deviceId], 'package_name' => ['$eq' => $packageName]],]]);
这是放置请求的代码
foreach ($documents['docs'] as $doc) {
$result = $client->PUT(
"/child_installed_apps/" . $doc['_id'] . '?rev=' .
urlencode($doc['_rev']), ['body' => json_encode($app_detail)]);
}
在放置这样的请求文件之前:
{
"id": "1dde9f973b42136c1921bb84d2bca33a",
"key": "1dde9f973b42136c1921bb84d2bca33a",
"value": {
"rev": "1-3ee7bd67f3552f68f7a7c4e4c8b2278f"
},"doc": {
"_id": "1dde9f973b42136c1921bb84d2bca33a",
"_rev": "1-3ee7bd67f3552f68f7a7c4e4c8b2278f",
"parent_id": "PV-407",
"child_device_id": "2525252525",
"app_name": "IMDb Movies & TV",
"app_category": "Entertainment",
"app_icon": "https://lh3.googleusercontent.com
/IKW5EWVX4qUYF6LPP1_O1sWiZsMblSQVnBrtkMAd07Br69lEApDez9yzFe09iAgJAg=s180-rw",
"package_name": "com.android.imdb",
"app_status": "0",
"all_block_app_status": "0",
"timely_status": "0",
"monday": "00:00",
"tuesday": "00:00",
"wednesday": "00:00",
"thursday": "00:00",
"friday": "00:00",
"saturday": "00:00",
"sunday": "00:00",
"app_datetime": "2019-01-15 17:20:46"
}
,并在请求文件后如下:
{
"id": "378647beffcb869ab4ad831196666059",
"key": "378647beffcb869ab4ad831196666059",
"value": {
"rev": "4-949230091e577dc3d89725836d35a71b"
},"doc": {
"_id": "378647beffcb869ab4ad831196666059",
"_rev": "4-949230091e577dc3d89725836d35a71b",
"app_status": "1"
}
}
答案 0 :(得分:0)
如果要进行部分更新,可以使用更新处理程序:
https://docs.couchdb.org/en/latest/ddocs/ddocs.html#update-functions
步骤1。使用更新处理程序创建设计文档
以下更新处理程序将更新文档的status属性,并将updated_at属性设置为当前时间。
{
"_id": "_design/myDesign",
"updates": {
"setStatus": "function(doc, req) {
// Use JSON.parse(req.body) if you use POST
doc.status = req.query.status
doc.updated_at = new Date().toISOString()
doc.updated_by = req.userCtx.name
return [doc, "Status was set to " + doc.status_]
}"
}
}
第2步。调用更新处理程序
GET /db/_design/myDesign/_update/setStatus/my_doc?status="NEW STATUS"
// Returns: "Status was set to NEW STATUS"
注意事项
如您所见,更新处理程序是用于部分更新或更新的出色工具,您不想进行往返以首先获取当前版本的文档。
警告:此方法并非没有冲突,您仍然可以引入冲突。