使用curl命令创建查询couchdb数据库
Posted sanqima
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用curl命令创建查询couchdb数据库相关的知识,希望对你有一定的参考价值。
couchdb是一个<key,value>类型的数据库,可以非常方便的存储键值对、json格式的数据。下面介绍,使用curl命令来创建、查询couchdb数据库,这里以创建一个名称为testplayer的数据库为例,进行说明。
1、创建testplayer数据库
通过PUT命令,在couchdb里,创建一个名称为testplayer的数据库。
curl -X PUT http://localhost:5984/testplayer
返回为:{“ok”:true}
2、添加数据
通过POST命令,给testplayer添加数据
curl -X POST http://localhost:5984/testplayer/_bulk_docs -H "Content-Type:application/json" -d '{"docs": [{"name":"Tom", "age": 10, "sex": "boy"}, {"name":"Jerry", "age": 25, "sex": "boy"}, {"name":"Mike", "age": 26, "sex": "boy"}, {"name":"Lucy", "age": 18, "sex": "girl"}, {"name":"Lily", "age": 35, "sex": "girl"}, {"name":"Eva", "age": 82, "sex": "girl"}, {"name":"Bill", "age": 50, "sex": "boy"}, {"name":"David", "age": 27, "sex": "boy"}, {"name":"Jim", "age": 75, "sex": "boy"}, {"name":"Henry", "age": 80, "sex": "boy"}]}'
返回如下:
[{“ok”:true,“id”:“ccf943db96c2d82986833aca7300a4c7”,“rev”:“1-d143438928b737ad6790e648d08ca020”},{“ok”:true,“id”:“ccf943db96c2d82986833aca7300af1c”,“rev”:“1-0f05b7b6fbdfb480fc4c7f3e9d0a96f3”},{“ok”:true,“id”:“ccf943db96c2d82986833aca7300ba65”,“rev”:“1-4ef40a23ff5699d2ece182c17484c4e9”},{“ok”:true,“id”:“ccf943db96c2d82986833aca7300bbf5”,“rev”:“1-06e9b6d66b5c34b0571eeb77b421c7b4”},{“ok”:true,“id”:“ccf943db96c2d82986833aca7300bd97”,“rev”:“1-1dbcbc85e784fb176ab2a0f32df0018e”},{“ok”:true,“id”:“ccf943db96c2d82986833aca7300c001”,“rev”:“1-9e25b1b1daa86d79ecc46c3e881954c5”},{“ok”:true,“id”:“ccf943db96c2d82986833aca7300c1b0”,“rev”:“1-e6497f65cc770ce353ef2336c2fafa8e”},{“ok”:true,“id”:“ccf943db96c2d82986833aca7300c76e”,“rev”:“1-933637d213fcab322371107047e94ede”},{“ok”:true,“id”:“ccf943db96c2d82986833aca7300cf20”,“rev”:“1-18c8aab5c9ac7f4263e164e2644ea31b”},{“ok”:true,“id”:“ccf943db96c2d82986833aca7300dacf”,“rev”:“1-866f6ad63b4a0addca1ef78291bb5905”}]
3、创建索引
创建age字段的索引,为后面的查询做准备。
curl -X POST http://localhost:5984/testplayer/_index -H "Content-Type:application/json" -d '{"index": {"fields": ["age"]}, "name": "age-index", "type": "json"}'
返回如下:
{“result”:“created”,“id”:"_design/22be4f97b7b32c324c53c021ba44a2a2529e1988",“name”:“age-index”}
4、开始查询
查询年龄在28~65之间的player,命令如下:
curl -X POST http://localhost:5984/testplayer/_find -H "Content-Type:application/json" -d '{"selector": {"age": {"$gte": 18, "$lte": 65}}, "fields": ["_id", "name", "age", "_rev"], "limit": 10, "skip": 0}'
返回如下:
{“docs”:[
{"_id":“ccf943db96c2d82986833aca7300bbf5”,“name”:“Lucy”,“age”:18,"_rev":“1-06e9b6d66b5c34b0571eeb77b421c7b4”},
{"_id":“ccf943db96c2d82986833aca7300af1c”,“name”:“Jerry”,“age”:25,"_rev":“1-0f05b7b6fbdfb480fc4c7f3e9d0a96f3”},
{"_id":“ccf943db96c2d82986833aca7300ba65”,“name”:“Mike”,“age”:26,"_rev":“1-4ef40a23ff5699d2ece182c17484c4e9”},
{"_id":“ccf943db96c2d82986833aca7300c76e”,“name”:“David”,“age”:27,"_rev":“1-933637d213fcab322371107047e94ede”},
{"_id":“ccf943db96c2d82986833aca7300bd97”,“name”:“Lily”,“age”:35,"_rev":“1-1dbcbc85e784fb176ab2a0f32df0018e”},
{"_id":“ccf943db96c2d82986833aca7300c1b0”,“name”:“Bill”,“age”:50,"_rev":“1-e6497f65cc770ce353ef2336c2fafa8e”}
],
“bookmark”: “g2wAAAACaAJkAA5zdGFydGtleV9kb2NpZG0AAAAgY2NmOTQzZGI5NmMyZDgyOTg2ODMzYWNhNzMwMGMxYjBoAmQACHN0YXJ0a2V5awABMmo”}
效果如下:
副录
couchdb支持使用网页方式来查看数据库,在浏览里输入testplayer的网址,然后按回车键即可,得到testplayer的数据库预览页面,如图(2)所示:
## 方法一:使用IP
http://192.168.xx.yy:5984/_utils/#database/testplayer/_all_docs
## 方法二:使用localhost
http://localhost:5984/_utils/#database/testplayer/_all_docs
以上是关于使用curl命令创建查询couchdb数据库的主要内容,如果未能解决你的问题,请参考以下文章