Basic CouchDB Queries

徘徊边缘 提交于 2020-01-05 20:01:50

问题


I've never worked with a database before, but I chose Couch DB because I needed a Json database, and HTTP queries seemed kinda simple. However the documentation assumes a level of knowledge I just don't have.

Assuming I have a database called 'subjects', it seems I can access the json by using GET on

http://localhost:5984/subjects/c6604f65029f1a6a5d565da029001f4c

However beyond that I'm stuck. Ideally I want to be able to:

  1. Access a list of all the keys in the database (not their values)
  2. Access an individual element by its key

Do I need to use views for this? Or can I just set fields in my GET request? Can someone give me a complete example of the request they'd use? Please don't link to the CouchDB documentation, it really hasn't helped me so far.


回答1:


Views can be used to fetch the data

1) In order to get all keys from the database you can use below view

function(doc) {
    if (doc.type=="article") 
        emit(doc._id,null); //emit(key,value), if you have any other field as key then specify as doc.key e.g doc.
}

You can access this view from browser using below URL

http://<ipaddress>:<port>/databasename/_design/designdocumentname/_view/viewname

e.g :

http://<ipaddress>:<port>/article/_design/articlelist/_view/articlelist

article is the database name,articlelist is name of the design document as well as view.

2) In order to access individual document by key Below view will return all the articles belonging to a particular department

 function(doc) {
  if(doc.type == 'article' ) {
    emit([doc.departmentname], doc);
  }
}

Query this view based on the "department name"

e.g: Get all the articles belonging to "IBU3" department

http://<ipaddress>:<port>/department/_design/categoryname/_view/categoryname?key=[%22IBU3%22]


来源:https://stackoverflow.com/questions/21302687/basic-couchdb-queries

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!