我是 CouchDB 的新手。我来自 .NET SQL Server 世界。
在浏览 CouchDB 的权威指南时,我感觉“这太棒了”。现在我正在测试我学到的一些东西,希望能在现实世界中实现它。
几周前我刚刚注册了一个 Cloudant 帐户,并开始使用它进行一些测试/学习。
在弄乱链接文档时,背后的整个理论看起来很简单,也是互联网上的海峡前锋示例。我想从具有一系列不同链接文档的文档中检索一些信息,这些文档本身具有链接文档数组。就像一个连接多对多关系表的多 SQL Server。你会看到下面的代码。希望这是有道理的。
以这个 SQL 查询为例。假设每个表中只有一个条目,我们应该返回一个记录,其中包含具有给定 sku 的鞋子的所有详细信息。但是,如果我们有多种鞋码,我们将不得不编写更多代码。
select ci.sku
,sc.color
,ss.size
,si.url
from CatalogItem ci
join ShoeImages si
on ci.sku = si.sku
and ci.sku = '656F-PINSEC12'
join ShoeSizes ss
on ci.sku = ss.sku
join ShoeColors sc
on ci.sku = sc.sku
我希望 CouchDB 通过 SKU 在https://username.cloudant.com/test/_design/catalogue/_view/item-details?include_docs=true&key=%22656F-PINSEC12%22返回以下 JSON
{
"_id": "689fe6982f4d604541db67ee4050a535",
"_rev": "5-64b5ddd751c51aadfcef1962c2c99c16",
"type": "catalogue-item",
"sku": "656F-PINSEC12",
"upc": "8549875231",
"shoe-colors":
[
{
"color": "black/houndstooth"
"shoe-sizes":
[
{
"size": 5,
"IsSizeAvailable": true
},
{
"size": 6,
"IsSizeAvailable": true
},
{
"size": 7,
"IsSizeAvailable": true
},
{
"size": 8,
"IsSizeAvailable": true
},
{
"size": 9,
"IsSizeAvailable": true
},
{
"size": 10,
"IsSizeAvailable": true
},
{
"size": 11,
"IsSizeAvailable": true
},
{
"size": 12,
"IsSizeAvailable": true
},
{
"size": 13,
"IsSizeAvailable": true
},
{
"size": 14,
"IsSizeAvailable": true
}
],
"shoe-images":
[
{
"full-images":
[
"http://www.someurl.com/full/656F-PINSEC12.jpg"
],
"thumbnail-images":
[
"http://www.someurl.com/thumb/656F-PINSEC12.jpg"
]
}
]
}
]
}
给定以下文档和 map/reduce:
//--catalog item
{
"_id": "689fe6982f4d604541db67ee4050a535",
"_rev": "5-64b5ddd751c51aadfcef1962c2c99c16",
"type": "catalogue-item",
"sku": "656F-PINSEC12",
"upc": "8549875231",
"shoe-colors": [
{
"_id": "bbbb92c3d61ed9f4f0e8111fb20fcf43",
"shoe-images": [
{
"_id": "7b547bae4ac911c6f05b97eba6cb355a"
}
],
"shoe-sizes": [
{
"_id": "12b6289d558d7ceb5bef725091666ce5"
}
]
}
]
}
//--shoe images
{
"_id": "7b547bae4ac911c6f05b97eba6cb355a",
"_rev": "4-4fde0cac1b4b8afc618bbba5b6669193",
"type": "shoe-images",
"sku": "656F-PINSEC12",
"color": "Black/Houndstoot",
"full-images": [
"http://www.someurl.com/full/656F-PINSEC12.jpg"
],
"thumbnail-images": [
"http://www.someurl.com/thumb/656F-PINSEC12.jpg"
]
}
//--shoe color
{
"_id": "bbbb92c3d61ed9f4f0e8111fb20fcf43",
"_rev": "2-e5d07c00a0261c231dd2be9b26a6c0dc",
"type": "shoe-color",
"sku": "656F-PINSEC12",
"color": "black/houndstooth"
}
//--shoe sizes
{
"_id": "12b6289d558d7ceb5bef725091666ce5",
"_rev": "2-192df709f9de1ef27e9e5f4404863bcc",
"type": "shoe-sizes",
"sku": "656F-PINSEC12",
"shoe-color": "black/houndstooth",
"shoe-sizes": [
{
"size": 5,
"IsSizeAvailable": true
},
{
"size": 6,
"IsSizeAvailable": true
},
{
"size": 7,
"IsSizeAvailable": true
},
{
"size": 8,
"IsSizeAvailable": true
},
{
"size": 9,
"IsSizeAvailable": true
},
{
"size": 10,
"IsSizeAvailable": true
},
{
"size": 11,
"IsSizeAvailable": true
},
{
"size": 12,
"IsSizeAvailable": true
},
{
"size": 13,
"IsSizeAvailable": true
},
{
"size": 14,
"IsSizeAvailable": true
}
]
}
//--map/reduce
{
"_id": "_design/catalog",
"_rev": "4-de5baf04b485768de12d78e5a0e5aa5e",
"views": {
"item": {
"map": "function(doc)
{
if (doc.type === 'catalog-item')
{
emit([doc.sku, doc], null);
if (doc.shoe-colors)
{
for (var sc in doc.shoe-colors)
{
emit([doc.sku, Number(sc)+1], {_id: doc.shoe-colors[sc]._id});
for (var si in doc.shoe-colors[sc].shoe-images)
{
emit([doc.sku, Number(si)+1], {_id: doc.shoe-colors[sc].shoe-images[si]._id});
}
for (var sz in doc.shoe-colors[sc].shoe-sizes)
{
emit([doc.sku, Number(sz)+1], {_id: doc.shoe-colors[sc].shoe-sizes[sz]._id});
}
}
}
}
}"
}
}
}
可能有更好的方法来实现这一点,但我想看看是否有可能拥有一个包含一系列链接文档的文档,这些文档也有一系列链接文档。但是我的 map/reduce 没有返回任何东西。它返回的只是:
{"total_rows":0,"offset":0,"rows":[
]}
我猜有人不会将所有信息存储在单个文档中,因为假设我们添加了新的展示尺码或将鞋码标记为不可用,这意味着必须将所有以前的值传回 CouchDB更新一个字段。
希望我的问题有意义 Oo__oO