couchdb-pythons ViewField中map函数的含义
我在我的一个项目中使用 couchdb.mapping 。我有一个名为 SupportCase
的类,派生自 Document
,其中包含我想要的所有字段。
我的数据库(称为 admin
)包含多种文档类型。我在所有文档中都有一个 type
字段,用于区分它们。我有许多 "case"
类型的文档,我想使用视图来获取它们。我有一个名为 support
的设计文档,其中有一个名为 cases
的视图。如果我使用 db.view("support/cases) 请求此视图的结果,我会得到一个包含我想要的内容的行列表。
但是,我想要以某种方式将其包装在 SupportCase
类中,以便我可以调用单个函数并获取我创建的系统中的所有 SupportCase
的列表。 code>ViewField 属性
@ViewField.define('cases')
def all(self, doc):
if doc.get("type","") == "case":
yield doc["_id"], doc
现在,如果我调用 SupportCase.all(db)
,我会返回所有案例,
我不明白的是该视图是否是预先计算并存储在中的 。数据库或类似于 db.query 的按需完成,如果是后者,速度会很慢,我想使用预先计算的视图,该怎么做?
I'm using the couchdb.mapping in one of my projects. I have a class called SupportCase
derived from Document
that contains all the fields I want.
My database (called admin
) contains multiple document types. I have a type
field in all the documents which I use to distinguish between them. I have many documents of type "case"
which I want to get at using a view. I have design document called support
with a view inside it called cases
. If I request the results of this view using db.view("support/cases)
, I get back a list of Row
s which have what I want.
However, I want to somehow have this wrapped by the SupportCase
class so that I can call a single function and get back a list of all the SupportCase
s in the system. I created a ViewField
property
@ViewField.define('cases')
def all(self, doc):
if doc.get("type","") == "case":
yield doc["_id"], doc
Now, if I call SupportCase.all(db)
, I get back all the cases.
What I don't understand is whether this view is precomputed and stored in the database or done on demand similar to db.query
. If it's the latter, it's going to be slow and I want to use a precomputed view. How do I do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为您需要的是:
Document
类有一个类方法view
,它按调用它的类包装行。因此,以下内容将返回一个ViewResult
,其中包含SupportCase
类型的行,并获取其中的.rows
给出支持案例列表。我认为您不需要了解 ViewField 魔法。但让我解释一下它是如何工作的。请考虑
CouchDB-python
文档中的以下示例。我认为这相当于:
将原始函数附加到
People.by_name.map_fun
。I think what you need is:
Document
class has a classmethodview
which wraps the rows by class on which it is called. So the following returns you aViewResult
with rows of typeSupportCase
and taking.rows
of that gives a list of support cases.And I don't think you need to get into the ViewField magic. But let me explain how it works. Consider the following example from the
CouchDB-python
documentation.I think this is equivalent to:
With the original function attached to
People.by_name.map_fun
.映射函数在某些方面类似于关系数据库中的索引。并不是每次都重做,并且当添加新文档时更新的方式不需要重做所有内容(这是一种树结构)。
这有一个非常好的摘要
The map function is in some ways analogous to an index in a relational database. It is not done again every time, and when new documents are added the way it is updated does not require everything to be redone (it's a kind of tree structure).
This has a pretty good summary
ViewField 使用预定义的视图,因此一旦构建,速度就会很快。它绝对不使用临时视图。
ViewField uses a pre-defined view so, once built, will be fast. It definitely doesn't use a temporary view.