gpt4 book ai didi

javascript - 查询文档之间的连接

转载 作者:行者123 更新时间:2023-11-30 17:02:17 25 4
gpt4 key购买 nike

我想在 CouchDb 中创建一个 View ,其中包含来自多个链接文档的一些字段。我的文件是这样的:

/*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. 使用query() API 和linked documents (在页面中搜索“链接文档”)
  2. 使用relational-pouch插件

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

编辑: 重新阅读您的问题,我发现您想将 3 种不同的文档类型结合在一起。目前这对于链接文档是不可能的(您只能“加入”两种类型),而关系袋是可能的。所以我想这使决定变得容易。 :)

关于javascript - 查询文档之间的连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28627771/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com