【问题标题】:Couch DB - Passing input parameters to viewCouch DB - 将输入参数传递给查看
【发布时间】:2014-12-30 15:51:56
【问题描述】:

我正在从我的 Web 应用程序(我的第一个应用程序)从 SQL 迁移到 Couch DB。

虽然我不能说为什么我不喜欢 SQL 查询,但我不确定我是否不喜欢,但发出 CURL 请求以访问我的数据库的想法肯定比使用 PHPs PDO 更好。

我花了一天半的时间尝试熟悉沙发 DB HTTP API。我不能说我已经通读了 API,但是在开始编码之前通读了 API。所以我的,可能是愚蠢的问题是 - how do I pass an variable other than doc to a map function while making a http request to the view. API 清楚地表明地图函数只接受一个参数,即“doc”,在这种情况下,下面的函数本身是错误的,但我在API 让我可以使用最终用户提供的输入来查询数据库。

我的地图功能是

function(doc, pid2){

      if (doc.pid === pid2)
    {
        emit(doc._id, doc) ;

    }
}

pid2 是一个由前端用户提供的数字。

    <?php
    $pid2 = file_get_contents(facebook graphi api call_returns a Profile ID) ;
    $user_exists = HTTP request to couch DB view to return 
in JSON format the list of JSON documents with pid = $pid2
?>

【问题讨论】:

    标签: mapreduce couchdb


    【解决方案1】:

    让你的视图发出以doc.pid为key的文档

    function(doc) { 
        emit(doc.pid, doc);
    }
    

    并使用key 参数检索正确的文档:

    http://localhost:5984/<database>/_design/<designdoc>/_view/<viewname>?key=<pid2>
    

    这应该返回所有带有doc.pid === pid2 的文档。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-05
      • 2020-08-12
      • 2015-12-07
      • 2016-08-21
      • 2022-06-10
      相关资源
      最近更新 更多