基本的CouchDB查询 [英] Basic CouchDB Queries

查看:298
本文介绍了基本的CouchDB查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前从未使用过数据库,但是我选择了Couch DB,因为我需要一个Json数据库,并且HTTP查询似乎很简单。但是,文档假设我只是没有一定的知识。

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.

假设我有一个名为 subjects的数据库,看来我可以使用 GET

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. 访问数据库中所有键的列表(而不是它们的值)

  2. 通过键访问单个元素

我是否需要为此使用视图?还是可以只在GET请求中设置字段?有人可以给我他们使用的请求的完整示例吗?请不要链接到CouchDB文档,到目前为止,它确实对我没有帮助。

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)为了从数据库中获取所有密钥,可以在下面的视图中使用

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.
}

您可以使用以下URL从浏览器访问此视图

You can access this view from browser using below URL

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

例如:

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

article是数据库名称,articlelist是设计文档以及视图的名称。

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

2)为了通过键
访问单个文档,下面的视图将返回所有属于特定部门的文章

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"

例如:获取属于 IBU3部门的所有文章

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

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

这篇关于基本的CouchDB查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆