查询文档之间的连接
Query with join between documents
我想在 CouchDb 中创建一个视图,其中包含来自多个链接文档的一些字段。
我的文档是这样的:
/*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"
},
我想要这样的结果:
/*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 世界,所以也许我在设计数据库时大错特错,所以欢迎任何有关如何更改文档结构的建议!
提前致谢!
弗朗切斯科
有两种方法可以做到这一点:
- 使用
query()
API 和 linked documents(在页面中搜索 "linked documents")
- 使用 relational-pouch 插件
relational-pouch 插件的优势在于,在底层,它比链接文档更快,因为它不依赖于建立 map/reduce 索引。链接文档的优点是它是 CouchDB 中解决这个问题的更传统的方法,而且它不依赖于额外的插件。选择你喜欢的任何一个。 :)
编辑: 重新阅读您的问题,我发现您想将 3 种不同的文档类型合并在一起。目前这对链接文档是不可能的(你只能 "join" 两种类型),而关系袋是可能的。所以我想这使决定变得容易。 :)
我想在 CouchDb 中创建一个视图,其中包含来自多个链接文档的一些字段。 我的文档是这样的:
/*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"
},
我想要这样的结果:
/*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 世界,所以也许我在设计数据库时大错特错,所以欢迎任何有关如何更改文档结构的建议!
提前致谢!
弗朗切斯科
有两种方法可以做到这一点:
- 使用
query()
API 和 linked documents(在页面中搜索 "linked documents") - 使用 relational-pouch 插件
relational-pouch 插件的优势在于,在底层,它比链接文档更快,因为它不依赖于建立 map/reduce 索引。链接文档的优点是它是 CouchDB 中解决这个问题的更传统的方法,而且它不依赖于额外的插件。选择你喜欢的任何一个。 :)
编辑: 重新阅读您的问题,我发现您想将 3 种不同的文档类型合并在一起。目前这对链接文档是不可能的(你只能 "join" 两种类型),而关系袋是可能的。所以我想这使决定变得容易。 :)