【问题标题】:Query with join between documents查询与文档之间的连接
【发布时间】:2017-01-26 17:24:01
【问题描述】:

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

提前致谢!

弗朗切斯科

【问题讨论】:

标签: javascript couchdb pouchdb


【解决方案1】:

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

  1. 使用query() API 和linked documents(在页面中搜索“链接文档”)
  2. 使用relational-pouch 插件

relational-pouch 插件的优势在于,在底层,它比链接文档更快,因为它不依赖于建立 map/reduce 索引。链接文档的优点是它是 CouchDB 中解决这个问题的更传统的方式,并且它不依赖于额外的插件。选择你喜欢的任何一个。 :)

编辑:重新阅读您的问题,我看到您想将 3 种不同的文档类型结合在一起。目前,链接文档无法做到这一点(您只能“加入”两种类型),而关系袋则可以。所以我想这让决定很容易。 :)

【讨论】:

  • 谢谢,我试试看!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-08
相关资源
最近更新 更多