curl操作CouchDB
couchdb 服务器地址: 127.0.0.1
端口:5984
添加数据库
-
连接到couchdb
curl -X GET http://127.0.0.1:5984
{"couchdb":"Welcome","uuid":"1c81fc63d761c82c4f48bac34afd5eb8","version":"1.6.0","vendor":{"name":"The Apache Software Foundation","version":"1.6.0"}}
-
创建一个数据库db1
curl -X PUT http://127.0.0.1:5984/db1
{"ok":true}
-
创建数据库 db2
curl -X PUT http://127.0.0.1:5984/db2
{"ok":true}
-
列出当前所有的数据库
curl -X GET http://127.0.0.1:5984/_all_dbs
["_replicator","_users","db1","db2"]
-
删除db2数据库
curl -X DELETE http://127.0.0.1:5984/db2
{"ok":true}
删除成功
数据库操作
-
向数据库添加数据
首先,获取一个uuid
curl -X GET http://127.0.0.1:5984/_uuids
{"uuids":["1925a2a284289df9b55b390525001ca1"]}
注意:每次得到的uuid不一样。
获取10个uuid: curl -X GET http://127.0.0.1:5984/_uuids?count=10使用uuid作为键插入一条数据
curl -X PUT http://127.0.0.1:5984/db1/1925a2a284289df9b55b390525001ca1 -d '{"title":"test","content":"this is test!"}'
{"ok":true,"id":"1925a2a284289df9b55b390525001ca1","rev":"1-4d3e6350fdcc39f7b482c4cab8ff5d9a"} -
更新记录
curl -X PUT http://127.0.0.1:5984/db1/1925a2a284289df9b55b390525001ca1 -d '{"title":"test","content":"this is test!modifyied!"}'
{"error":"conflict","reason":"Document update conflict."}
失败了。因为,couchdb是按版本提交的,同一个源提交多次会造成一定的混乱。所以,其采用了版本进行控制。curl -X PUT http://127.0.0.1:5984/db1/1925a2a284289df9b55b390525001ca1 -d '{"_rev":"1-4d3e6350fdcc39f7b482c4cab8ff5d9a","title":"test","content":"this is test!"}'
{"ok":true,"id":"1925a2a284289df9b55b390525001ca1","rev":"2-f6f24194b29981316f2412e288cda320"}
这样就没有问题了。 -
获取记录
curl -X GET http://127.0.0.1:5984/db1/1925a2a284289df9b55b390525001ca1
{"_id":"1925a2a284289df9b55b390525001ca1","_rev":"2-f6f24194b29981316f2412e288cda320","title":"test","content":"this is test!"}
-
列出db1库下的所有文档
curl -X GET http://127.0.0.1:5984/db1/_all_docs
{"total_rows":2,"offset":0,"rows":[
{"id":"1925a2a284289df9b55b390525001ca1","key":"1925a2a284289df9b55b390525001ca1","value":{"rev":"2-f6f24194b29981316f2412e288cda320"}},
{"id":"1925a2a284289df9b55b390525002c29","key":"1925a2a284289df9b55b390525002c29","value":{"rev":"1-4a1158c264f8cc636e1fc54a7a696de6"}}
]} -
删除记录
curl -X DELETE http://127.0.0.1:5984/db1/1925a2a284289df9b55b390525001ca1?rev=2-f6f24194b29981316f2412e288cda320
上传附件
curl -X PUT http://127.0.0.1:5984/db1/40dab8a067c56aef572307dd1f0119c8 -d '{"title":"test","content":"this is test!"}'
curl -X PUT http://127.0.0.1:5984/db1/40dab8a067c56aef572307dd1f0119c8/atwork.jpg?rev=2-2739352689 --data-binary @atwork.jpg -H "Content-Type:image/jpg"
atwork.jpg是当前目录下的图片文件。
数据库复制
-
本地复制
curl -X PUT http://127.0.0.1:5984/albums-replica
curl -X POST http://127.0.0.1:5984/_replicate -d '{"source":"albums","target":"albums-replica"}' -H "Content-Type: application/json" -
本地到远端复制
curl -X POST http://127.0.0.1:5984/_replicate -d '{"source":"albums","target":"http://example.org:5984/albums-replica"}' -H "Content-Type:application/json"
如果远端服务器有密码,可以采用这种格式:http://username:pass@remotehost:5984/demo
-
远端到本地
curl -X POST http://127.0.0.1:5984/_replicate -d '{"source":"http://example.org:5984/albums-replica","target":"albums"}' -H "Content-Type:application/json"
-
远端到远端
curl -X POST http://127.0.0.1:5984/_replicate -d '{"source":"http://example.org:5984/albums","target":"http://example.org:5984/albums-replica"}' -H "Content-Type: application/json"
本文github地址:
https://github.com/mike-zhang/mikeBlogEssays/blob/master/2014/20141007_curl操作couchDB.md
欢迎补充
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)