CouchDB笔记

安装

  • docker的方式安装
    docker run -p 5984:5984 -d --name my-couchdb -e COUCHDB_USER=admin -e COUCHDB_PASSWORD=password -v /opt/couchdb/data:/opt/couchdb/data couchdb:2.1.1
  • 基本操作
    • 如果数据库有密码密码
      • 使用-u参数
        curl -X GET http://localhost:5984/mychannel_mycc/_all_docs -u "admin:password" --header 'content-type: application/json'
      • 使用@
        curl -X GET http://admin:password@localhost:5984/mychannel_mycc/_all_docs --header 'content-type: application/json'

索引

couchdb官网文档

索引类型可以是“json”或“text”。默认为JSON。未来将支持地理空间索引。可选文本索引通过第三方库支持可选文本索引

  • 全局索引

针对所有的文档的索引
默认情况下,JSON索引将包括所有具有索引字段的文档,包括那些具有空值的文档。

{
	"index": {  //索引的定义
		"fields": ["docType", "owner"]
	},
	"ddoc": "indexOwnerDoc", //将要创建的设计文档的名称
	"name": "indexOwner",//索引的名称
	"type": "json" //索引的类型
}
  • 部分索引
{
  "index": {
    "partial_filter_selector": { //用于过滤,只在部分文档上建立索引
      "status": {
        "$ne": "archived"
      }
    },
    "fields": ["type"]
  },
  "ddoc" : "type-not-archived",
  "type" : "json"
}

创建索引

curl -i -X POST -H "Content-Type: application/json" -d '{"index":{"fields":["docType","owner"]},"ddoc":"indexOwnerDoc","name":"indexOwner","type":"json"}' http://localhost:5984/mychannel_mycc/_index

查询

根据key查询某一个文档
curl -X GET http://localhost:5984/mychannel_mycc/key -u "admin:password"
条件查询

curl -X POST http://localhost:5984/mychannel_mycc/_find -d '{"selector":{"name":"name1","docType":"type1"}}' -u "admin:password" --header 'content-type: application/json'

查询时使用的索引

若不明确指定,也会自动使用(如果存在)
curl -X GET http://localhost:5984/mychannel_mycc/ -d "{"selector":{"docType":"ent_token","ent_id":"1001"},"use_index":["_design/indexEntIdDoc", "indexEntId"]}" -u "admin:password"

展示查询的时候使用的索引

展示所用索引
curl -X POST http://localhost:5984/mychannel_mycc/_explain -d '{"selector":{"docType":"ent_token","ent_id":"1001"} ,"use_index":["_design/indexEntIdDoc", "indexEntId"]}' -u "admin:password" -H "Content-Type:application/json"

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值