查询文档之间的连接

时间:2015-02-20 11:21:34

标签: javascript couchdb pouchdb

我想在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世界,所以也许我设计的数据库非常错误,所以欢迎任何有关如何更改文档结构的建议!

提前致谢!

弗朗西斯

1 个答案:

答案 0 :(得分:2)

有两种方法可以做到这一点:

  1. 使用query() API和linked documents(搜索"链接文档的页面")
  2. 使用relational-pouch插件
  3. 关系袋插件的优点在于,它比链接文档更快,因为它不依赖于构建map / reduce索引。链接文档的优势在于它是在CouchDB中解决此问题的更传统方式,并且它不依赖于额外的插件。选择你喜欢的任何一个。 :)

    编辑:重新阅读您的问题,我看到您想要将3种不同的文档类型连接在一起。目前,链接文档无法实现(您只能"加入"两种类型),而关系文件则可以。所以我想这使决策变得容易。 :)

相关问题