使用视图获取与主域名相同的后缀



我有一些文档,如何使用视图来获取其电子邮件地址具有相同域名的文档。 像所有带有@gmail.com 或@yahoo.com 的文档一样,如果endkey可以得到这个结果?

这是我在地图上写的视图,但我认为这不是个好主意

  function(doc) {
    for (var i in doc.emails) {
     if (doc.emails[i].emailAddress.toLowerCase().indexOf("@yahoo.ibm.com")!=-1) {
                    emit(doc.emails[i].emailAddress.toLowerCase(), doc);
                }
            }
        }
    }

为了清楚起见,endkey 参数不是寻找后缀。开始键和结束键就像要获取的键的限制。例如,您可以将 id 为 1 的文档获取为 id 10 的文档startkey="1"&endkey="10"

在您的情况下,您希望创建一个视图,该视图将按域名对文档进行分组。我创建了一个具有byDomain视图的设计文档。映射函数如下所示:

    function(doc){
        if(doc.email){ //I used the document's property email for my view.
            //Now, we will emit an array key. The first value will be the domain.
            //To get the domain, we split the string with the character '@' and we take what comes after.
            //Feel free to add more validations
            //The second key will be the document id. We don't emit any values. It's faster to simply add
            //the includes_docs query parameter.
            emit([doc.email.split('@')[1],doc._id]); 
        }
    }
  1. 让我们查询我的所有文档以向您展示我所拥有的内容

请求 : http://localhost:5984/test/_all_docs?include_docs=true响应:

    {"total_rows":4,"offset":0,"rows":[
    {"id":"7f34ec3b9332ab4e555bfca202000e5f","key":"7f34ec3b9332ab4e555bfca202000e5f","value":{"rev":"1-c84cf3bf33e1d853f99a4a5cb0a4af74"},"doc":{"_id":"7f34ec3b9332ab4e555bfca202000e5f","_rev":"1-c84cf3bf33e1d853f99a4a5cb0a4af74","email":"steve@gmail.com"}},
    {"id":"7f34ec3b9332ab4e555bfca202001101","key":"7f34ec3b9332ab4e555bfca202001101","value":{"rev":"1-53a8a9f2a24d812fe3c98ad0fe020197"},"doc":{"_id":"7f34ec3b9332ab4e555bfca202001101","_rev":"1-53a8a9f2a24d812fe3c98ad0fe020197","email":"foo@example.com"}},
    {"id":"7f34ec3b9332ab4e555bfca202001b02","key":"7f34ec3b9332ab4e555bfca202001b02","value":{"rev":"1-cccec02fe7172fb637ac430f0dd25fa2"},"doc":{"_id":"7f34ec3b9332ab4e555bfca202001b02","_rev":"1-cccec02fe7172fb637ac430f0dd25fa2","email":"bar@gmail.com"}},
    {"id":"_design/emails","key":"_design/emails","value":{"rev":"4-76785063c7dbeec96c495db76a8faded"},"doc":{"_id":"_design/emails","_rev":"4-76785063c7dbeec96c495db76a8faded","views":{"byDomain":{"map":"ttfunction(doc){ntttif(doc.email){ //I used the document's property email for my view.ntttt//Now, we will emit an array key. The first value will be the domain.ntttt//To get the domain, we split the string with the character '@' and we take what comes after.ntttt//Feel free to add more validationsntttt//The second key will be the document id. We don't emit any values. It's faster to simply addntttt//the includes_docs query parameter.nttttemit([doc.email.split('@')[1],doc._id]); nttt}ntt}"}},"language":"javascript"}}
    ]}

如您所见,我得到了一些带有属性"电子邮件"设置的极简主义文档。

  1. 让我们在没有任何参数的情况下查询我的视图

请求 : http://localhost:5984/test/_design/emails/_view/byDomain

响应:

{"total_rows":3,"offset":0,"rows":[
    {"id":"7f34ec3b9332ab4e555bfca202001101","key":["example.com","7f34ec3b9332ab4e555bfca202001101"],"value":null},
    {"id":"7f34ec3b9332ab4e555bfca202000e5f","key":["gmail.com","7f34ec3b9332ab4e555bfca202000e5f"],"value":null},
    {"id":"7f34ec3b9332ab4e555bfca202001b02","key":["gmail.com","7f34ec3b9332ab4e555bfca202001b02"],"value":null}
    ]}
  1. 让我们仅查询具有 gmail.com 域的文档。

请求 : http://localhost:5984/test/_design/emails/_view/byDomain?startkey=["gmail.com"]&endkey=["gmail.com","ufff0"]

结果:

    {"total_rows":3,"offset":1,"rows":[
        {"id":"7f34ec3b9332ab4e555bfca202000e5f","key":["gmail.com","7f34ec3b9332ab4e555bfca202000e5f"],"value":null},
        {"id":"7f34ec3b9332ab4e555bfca202001b02","key":["gmail.com","7f34ec3b9332ab4e555bfca202001b02"],"value":null}
        ]}  

你可以使用一个简单的地图函数来实现这一点:

function (doc) {
  var domain = doc.email.split('@').pop();
  // this logic is fairly hack-ish, you may want to be more sophisticated
  emit(domain);
}

然后,您只需传递key=gmail.com即可从视图中获得所需的结果。我还会添加include_docs=true,而不是将整个文档作为您的值发出。

您可以在官方 CouchDB 文档中阅读有关视图的更多信息。

相关内容

  • 没有找到相关文章

最新更新