好吧,我终于明白你想要做什么。是的,这是可能的。这是方法。
您有 2 个文件
{
"_id":"anyvalue",
"type": "track",
"title": "Bohemian Rhapsody"
}
{
"_id":"2",
"type": "artist",
"name": "Queen",
"tracks": ["anyvalue"]
}
您做错的事情是没有在曲目值(数组中的项目)周围加上引号。
2)引用 id 必须是 _id 才能工作。区别值得注意,因为您可以有 id 字段,但只有 _id 用于标识文档。
对于你想要的结果,这个视图就足够了
function(doc) {
if (doc.type === 'artist') {
for (var i in doc.tracks) {
var id = doc.tracks[i];
emit(id, { _id: id });
}
}
}
您想要做的是在 for 循环内使用发射函数来发射每个艺术家的“曲目”的 id 字段。
然后您想要使用 include_docs=true 参数查询 couch 数据库视图。这是您在 iris couch 上创建的数据库的最终结果。
{
"total_rows": 3,
"offset": 0,
"rows": [
{
"id": "0b86008d8490abf0b7e4f15f0c6a50a7",
"key": "0b86008d8490abf0b7e4f15f0c6a463b",
"value": {
"_id": "0b86008d8490abf0b7e4f15f0c6a463b"
},
"doc": {
"_id": "0b86008d8490abf0b7e4f15f0c6a463b",
"_rev": "3-7e4ba3bfedd29a07898125c09dd7262e",
"type": "track",
"title": "Boheniam Rhapsody"
}
},
{
"id": "0b86008d8490abf0b7e4f15f0c6a50a7",
"key": "0b86008d8490abf0b7e4f15f0c6a5ae2",
"value": {
"_id": "0b86008d8490abf0b7e4f15f0c6a5ae2"
},
"doc": {
"_id": "0b86008d8490abf0b7e4f15f0c6a5ae2",
"_rev": "2-b3989dd37ef4d8ed58516835900b549e",
"type": "track",
"title": "Another one bites the dust"
}
},
{
"id": "0b86008d8490abf0b7e4f15f0c6a695e",
"key": "0b86008d8490abf0b7e4f15f0c6a6353",
"value": {
"_id": "0b86008d8490abf0b7e4f15f0c6a6353"
},
"doc": {
"_id": "0b86008d8490abf0b7e4f15f0c6a6353",
"_rev": "2-0383f18c198b813943615d2bf59c212a",
"type": "track",
"title": "Stripper Vicar"
}
}
]
}
Jason 在这篇文章中对此进行了精彩的解释
在 CouchDB 中进行一对多“JOIN”的最佳方式 https://stackoverflow.com/questions/3033443/best-way-to-do-one-to-many-join-in-couchdb
此链接对于 couchdb 中的实体关系也很有帮助
http://wiki.apache.org/couchdb/EntityRelationship http://wiki.apache.org/couchdb/EntityRelationship