couchdb view using another view?

不问归期 提交于 2019-12-10 10:06:45

问题


I have got questions about views in couchdb At the moment, I have a number of views (e.g. view_A, view_B.... view_Z), for each view they contains same range of keys but with different values. ie:

view_A = {"key":"key_1", "value":10}, {"key":"key_2", "value":100}
view_B = {"key":"key_1", "value":5}, {"key":"key_2", "value":2}
view_C = {"key":"key_1", "value":1}, {"key":"key_2", "value":2}

I am expecting to have a view to represent values in view_A divided by values in view_B =>

view_A_over_B = {"key":"key_1", "value":2}, {"key":"key_2", "value":50}

A view to represent values in view_C times values in view_B =>

view_C_times_B = {"key":"key_1", "value":5}, {"key":"key_2", "value":4}

Would this be possible to have a map/reduce function which is calling the views and do the calculation as mentioned like above?


回答1:


Views in CouchDB can only access the current document being processed and cannot access other documents, or data from other views. So, unfortunately, it is not possible using views in CouchDB to build the functionality you want.

As it's not clear how you're building the views, it may be possible to use List functions instead of views to build the results you need. List functions are more capable, but you're then responsible for outputting the results (as HTML, Json, etc.).




回答2:


View function run just once during document save.

You can define javascript functions that calculate desired numbers, than include the following in your views:

view_A:

...
emit(doc._id, funcA(doc));
...

view_B:

...
emit(doc._id, funcB(doc));
...

view_C:

...
emit(doc._id, funcC(doc));
...

view_A_over_B:

...
emit(doc._id, funcA(doc)/funcB(doc));
...

view_C_times_B:

...
emit(doc._id, funcC(doc)*funcB(doc));
...

You can use CommonJS Modules to define funcA, funcB and funcC. Or use obsolete macro.




回答3:


There was already mentioned that it's not possible to use view against other views, maximum you may apply some list function to specific view (with cost of full scan processing complexity), but still you may look at third-party tools like Couch-Incarnate for chained views support. Also, Cloudant has support for chained views.



来源:https://stackoverflow.com/questions/18079917/couchdb-view-using-another-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!