如何为以下情况设计 couchdb 视图?

发布于 2024-12-09 06:40:05 字数 322 浏览 0 评论 0原文

我正在将应用程序从 mySQL 迁移到 couchDB。 (好吧,请不要对此做出判断)。

有一个带有签名的函数

getUserBy($column, $value) 

现在您可以看到,对于 SQL,构建查询并触发它是一项微不足道的工作。

然而,就 couchDB 而言,我应该使用地图函数编写视图,

目前我有很多视图,例如

get_user_by_name
get_user_by_email 

等等。谁能建议一种更好且可扩展的方法来做到这一点?

I am migrating an application from mySQL to couchDB. (Okay, Please dont pass judgements on this).

There is a function with signature

getUserBy($column, $value) 

Now you can see that in case of SQL it is a trivial job to construct a query and fire it.

However as far as couchDB is concerned I am supposed to write views with map functions

Currently I have many views such as

get_user_by_name
get_user_by_email 

and so on. Can anyone suggest a better and yet scalable way of doing this ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猫七 2024-12-16 06:40:05

当然!由于其强大功能,我最喜欢的视图之一是 by_field。这是一个非常简单的地图功能。

function(doc) {
    // by_field: map function
    // A single view for every field in every document!
    var field, key;

    for (field in doc) {
        key = [field, doc[field]];
        emit(key, 1);
    }
}

假设您的文档有一个 .name 字段作为其姓名,并使用 .email 字段作为其电子邮件地址。

按姓名获取用户(例如“Alice”和“Bob”):

GET /db/_design/example/_view/by_field?include_docs=true&key=["name","Alice"]
GET /db/_design/example/_view/by_field?include_docs=true&key=["name","Bob"]

通过电子邮件获取用户,从同一视图:

GET /db/_design/example/_view/by_field?include_docs=true&key=["email","[email protected]"]
GET /db/_design/example/_view/by_field?include_docs=true&key=["name","[email protected]"]

我喜欢发出 1 的原因是这样您可以稍后编写reduce 函数使用 sum() 轻松添加与您的查询匹配的文档。

Sure! One of my favorite views, for its power, is by_field. It's a pretty simple map function.

function(doc) {
    // by_field: map function
    // A single view for every field in every document!
    var field, key;

    for (field in doc) {
        key = [field, doc[field]];
        emit(key, 1);
    }
}

Suppose your documents have a .name field for their name, and .email for their email address.

To get users by name (ex. "Alice" and "Bob"):

GET /db/_design/example/_view/by_field?include_docs=true&key=["name","Alice"]
GET /db/_design/example/_view/by_field?include_docs=true&key=["name","Bob"]

To get users by email, from the same view:

GET /db/_design/example/_view/by_field?include_docs=true&key=["email","[email protected]"]
GET /db/_design/example/_view/by_field?include_docs=true&key=["name","[email protected]"]

The reason I like to emit 1 is so you can write reduce functions later to use sum() to easily add up the documents that match your query.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文