Map CouchDB使用tag=foo或tag=bar查看文档

Map CouchDB使用tag=foo或tag=bar查看文档,map,couchdb,Map,Couchdb,鉴于我的以下文件: { _id: ###, type: "blogpost", title: "First blog post", tag: "tutorial" } { _id: ###, type: "blogpost", title: "Second blog post", tag: "report" } { _id: ###, type: "blogpost", title: "Third blog post",

鉴于我的以下文件:

{
   _id: ###,
   type: "blogpost",
   title: "First blog post",
   tag: "tutorial"
}

{
   _id: ###,
   type: "blogpost",
   title: "Second blog post",
   tag: "report"
}

{
   _id: ###,
   type: "blogpost",
   title: "Third blog post",
   tag: "tutorial"
}

{
   _id: ###,
   type: "blogpost",
   title: "Fourth blog post",
   tag: "article"
}
现在,我想做的是:找到所有blogpost,哪个标签是article或report


我已经在文档中读到,我能够对视图或列表执行POST,允许搜索多个键。但这总是需要卷曲或其他什么,对吗

您需要创建至少一个视图才能使其正常工作。我对你的需求不是很清楚,所以我只举一个简单的例子,不考虑它是实现你想要的东西的最佳方式

访问Futon,并在数据库中创建视图。您只需要一个映射函数即可完成所需的操作:

function(doc) {
    if (doc.tag && (doc.tag == "article" || doc.tag == "report")) {
        emit(doc.tag, doc);
    }
}
如果保存视图并将其称为articles\u或\u reports,则可以通过以下请求从视图中获取数据:

http://yourserver.com:5984/db/_design/test/_view/articles_or_reports
哪个会回来

{"total_rows":2,"offset":0,"rows":[
{"id":"168ba21f7209994b69336dd8c30041f3","key":"article","value":{"_id":"168ba21f7209994b69336dd8c30041f3","_rev":"1-f033b30522d09c33cbd68332b89c95a7","type":"blogpost","title":"Fourth blog post","tag":"article"}},
{"id":"168ba21f7209994b69336dd8c3003139","key":"report","value":{"_id":"168ba21f7209994b69336dd8c3003139","_rev":"1-f7a473afc253972f7cfec62a335dcb23","type":"blogpost","title":"Second blog post","tag":"report"}}
]}
但这总是需要卷曲或其他什么,对吗


这取决于您的应用程序,但CouchDB的唯一接口是通过HTTP。有许多语言库可以将您的交互提升到一个更高的层次。

您需要创建至少一个视图才能实现此功能。我对你的需求不是很清楚,所以我只举一个简单的例子,不考虑它是实现你想要的东西的最佳方式

访问Futon,并在数据库中创建视图。您只需要一个映射函数即可完成所需的操作:

function(doc) {
    if (doc.tag && (doc.tag == "article" || doc.tag == "report")) {
        emit(doc.tag, doc);
    }
}
如果保存视图并将其称为articles\u或\u reports,则可以通过以下请求从视图中获取数据:

http://yourserver.com:5984/db/_design/test/_view/articles_or_reports
哪个会回来

{"total_rows":2,"offset":0,"rows":[
{"id":"168ba21f7209994b69336dd8c30041f3","key":"article","value":{"_id":"168ba21f7209994b69336dd8c30041f3","_rev":"1-f033b30522d09c33cbd68332b89c95a7","type":"blogpost","title":"Fourth blog post","tag":"article"}},
{"id":"168ba21f7209994b69336dd8c3003139","key":"report","value":{"_id":"168ba21f7209994b69336dd8c3003139","_rev":"1-f7a473afc253972f7cfec62a335dcb23","type":"blogpost","title":"Second blog post","tag":"report"}}
]}
但这总是需要卷曲或其他什么,对吗


这取决于您的应用程序,但CouchDB的唯一接口是通过HTTP。有许多语言库可以将您的交互提高到一个更高的层次。

如果您需要在HTTP调用中指定文章、报告作为变量,那么您只需要一个视图和一个列表

视图:测试视图

function(doc) {
  if (doc.tag) {
    emit(doc.tag, doc);
  }
};
列表:测试列表

function(head, req) {
  start({
    "headers": {
      "Content-Type": "text/html"
    }
  });
  var row;
  var tags = req.query.tags;
  var tagArray = tags.split(',');
  while(row = getRow()) {
    if (tagArray.indexOf(row.key) != -1) {
      send(row.value.tag + ' : ' + row.value.title + '<br>');
    }
  }
}
功能(头部,需求){
开始({
“标题”:{
“内容类型”:“文本/html”
}
});
var行;
var tags=req.query.tags;
var tagArray=tags.split(',');
while(row=getRow()){
if(tagArray.indexOf(row.key)!=-1){
发送(row.value.tag+':'+row.value.title+'
); } } }
现在,您可以使用

如果您需要在HTTP调用中指定article、report作为变量,那么您只需要一个视图和一个列表

视图:测试视图

function(doc) {
  if (doc.tag) {
    emit(doc.tag, doc);
  }
};
列表:测试列表

function(head, req) {
  start({
    "headers": {
      "Content-Type": "text/html"
    }
  });
  var row;
  var tags = req.query.tags;
  var tagArray = tags.split(',');
  while(row = getRow()) {
    if (tagArray.indexOf(row.key) != -1) {
      send(row.value.tag + ' : ' + row.value.title + '<br>');
    }
  }
}
功能(头部,需求){
开始({
“标题”:{
“内容类型”:“文本/html”
}
});
var行;
var tags=req.query.tags;
var tagArray=tags.split(',');
while(row=getRow()){
if(tagArray.indexOf(row.key)!=-1){
发送(row.value.tag+':'+row.value.title+'
); } } }
现在,您可以使用

谢谢你的回答。我确实已经创建了一个视图和列表。但是,我需要标签是变量。例如:“.显然这不起作用,但我需要类似的东西,而不使用post.Thanx作为您的答案。我确实已经创建了一个视图和列表。但是,我需要标记成为变量。因此,类似于:”。显然,这不起作用,但我需要这样的东西,而不使用post。嗯,这正是我想要的。我的javascript知识还不足以让我自己弄明白这一点。谢谢你的帮助!那正是我想要的。我的javascript知识还不足以让我自己弄明白这一点。谢谢你的帮助!