Query with join between documents
我想在 CouchDb 中创建一个视图,其中包含来自多个链接文档的一些字段。
我的文件是这样的:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | /*Products:*/ { "_id":"Products:ABC", "doctype":"Products", "productCode":"ABC", "description":"The best product you ever seen", "category_id":"Categories:1", "brand_id":"Brands:52" }, { "_id":"Products:DEF", "doctype":"Products", "productCode":"DEF", "description":"DEFinitely a good product", "category_id":"Categories:2", "brand_id":"Brands:53" }, /*Categories*/ { "_id":"Categories:1", "categoryID":"1", "description":"Awesome products" }, { "_id":"Categories:2", "categoryID":"2", "description":"Wonderful supplies" }, /*Brands*/ { "_id":"Brands:52", "brandID":"52", "description":"Best Items" }, { "_id":"Brands:53", "brandID":"53", "description":"Great Gadgets" }, |
我想要这样的结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | /*View results: */ { "id":"Products:ABC", "key":"Products:ABC", "value": { "productCode":"ABC", "description":"The best product you ever seen", "category": { "categoryID":"1", "description":"Awesome products" }, "brand": { "brandID":"52", "description":"Best Items" } } }, { "id":"Products:DEF", "key":"Products:DEF", "value": { "productCode":"DEF", "description":"DEFinitely a good product", "category": { "categoryID":"2", "description":"Wonderful supplies" }, "brand": { "brandID":"53", "description":"Great Gadgets" } } }, |
我们的目标是获得一个连接三个文档的结果。有可能吗?
你可以想象我来自 SQL 世界,所以也许我设计的数据库非常错误,所以欢迎任何关于如何更改文档结构的建议!
提前致谢!
弗朗切斯科
有两种方法可以做到这一点:
relational-pouch 插件的优势在于,在底层,它比链接文档更快,因为它不依赖于建立 map/reduce 索引。链接文档的优点是它是 CouchDB 中解决这个问题的更传统的方式,并且它不依赖额外的插件。选择你喜欢的任何一个。 :)
编辑:重新阅读您的问题,我看到您想将 3 种不同的文档类型结合在一起。目前,链接文档无法做到这一点(您只能"加入"两种类型),而关系袋则可以。所以我想这让决定很容易。 :)