CouchDB:将新数组插入到文档中

时间:2011-09-09 09:46:48

标签: arrays couchdb add

我有一份由以下人员组成的文件:

{
"_id": "00001.74365CF0449457AA5FB52822DBE1F22A",
"_rev": "1-1b976f3adb75c220aff28b4c69f41e18",
"game": "UT411",
"guid": "74365CF0449457AA5FB52822DBE1F22A",
"sid": "00001",
"playerinfo": [
   {
       "timestamp": "1315503699.777494167",
       "name": "Elisa",
       "ip": "87.66.181.166",
       "gear": "FMAOSTA",
       "weapmodes": "01000110220000020000",
       "isp": "ADSL-GO-PLUS",
       "geoloc": "Hotton:50.266701:5.450000",
       "sid": "00001"
   }
   ]
}

我想要实现的是向playerinfo数组添加信息,以使我的文档看起来像这样

{
"_id": "00001.74365CF0449457AA5FB52822DBE1F22A",
"_rev": "1-1b976f3adb75c220aff28b4c69f41e18",
"game": "UT411",
"guid": "74365CF0449457AA5FB52822DBE1F22A",
"sid": "00001",
"playerinfo": [
   {
       "timestamp": "1315503699.777494167",
       "name": "Elisa",
       "ip": "87.66.181.166",
       "gear": "FMAOSTA",
       "weapmodes": "01000110220000020000",
       "isp": "ADSL-GO-PLUS",
       "geoloc": "Hotton:50.266701:5.450000",
       "sid": "00001"
   },
   {
       "timestamp": "1315503739.234334167",
       "name": "Elisa-new",
       "ip": "87.66.181.120",
       "gear": "FMAGGGA",
       "weapmodes": "01000110220000020000",
       "isp": "ADSL-GO-PLUS",
       "geoloc": "Hotton:50.266701:5.450000",
       "sid": "00001"
   }
   ]
}

有没有办法用html PUT做到这一点?

谢谢!

1 个答案:

答案 0 :(得分:7)

简单的答案是获取JSON文档,/example_db/00001.74365CF0449457AA5FB52822DBE1F22A然后修改内容,然后将其放回服务器,返回/example_db/00001.74365CF0449457AA5FB52822DBE1F22A

CouchDB支持一种名为update function的快捷方式技术。原理是一样的,除了CouchDB将获取文档,进行任何更改,然后再次存储 - 全部在服务器端。

我建议你从前一种更简单的技术开始。接下来,您可以在必要时重构以使用服务器端_update函数。