视图中CouchDB的链接文档 [英] CouchDB's Linked Documents in a View
问题描述
我很难理解CouchDB的链接文档功能。
I'm having a hard time getting my head around CouchDB's linked documents feature.
我有两种类型
数据存储在单个CouchDB数据库中:
I have two types
of data being stored in a single CouchDB database:
{
"id":"1",
"type": "track",
"title": "Bohemian Rhapsody"
}
{
"id":"2",
"type": "artist",
"name": "Queen",
"tracks": ["1"]
}
我的印象是,我可以像下面这样写一个视图,并发出以下文档:
I'm under the impression that I can write a view like the one below and get the following documents emited:
{
"id":"2",
"type": "artist",
"name": "Queen",
"tracks": [
{
"id":"1",
"type": "track",
"title": "Bohemian Rhapsody"
}
]
}
我一直在尝试这种视图,但是它没有按照我期望的方式工作:
I've been trying this view, but it's not working the way I'm expecting:
function(doc) {
if(doc.type == 'artist') {
var tracks = [];
for(var i = 0; i < doc.tracks.length; i++) {
tracks.push({_id:doc.tracks[i]});
}
newdoc = eval(uneval(doc));
newdoc.tracks = tracks;
emit(doc._id,newdoc);
}
}
此处为示例: http://jphastings.iriscouch.com/_utils/database.html?music/_design/test / _view / linked
这并没有返回我希望的-您有什么建议吗?谢谢
This isn't returning what I'd hope - do you have any suggestions? Thanks
推荐答案
好吧,我终于明白了您要做什么,是可以的。这就是方法。
Okay I finally understand what you are trying to do.Yes this is possible.Here is how.
您有2个文档
{
"_id":"anyvalue",
"type": "track",
"title": "Bohemian Rhapsody"
}
{
"_id":"2",
"type": "artist",
"name": "Queen",
"tracks": ["anyvalue"]
}
您做错了的是没有在音轨值(数组中的项)周围加引号。
What you were doing wrong was not having quotes around the value of tracks(the item in the array).
2)引用ID必须是_id才能起作用。区别是值得注意的,因为您可以具有id字段,但仅使用_id来标识文档。
2)The reference id must be _id for this to work.The difference is worth noting since you can have id field but only _id are used to identify documents.
对于结果,您希望此视图足够
For the result you want this view would suffice
function(doc) {
if (doc.type === 'artist') {
for (var i in doc.tracks) {
var id = doc.tracks[i];
emit(id, { _id: id });
}
}
}
您想做什么是在for循环内使用emit函数来发出每个艺术家的曲目的id字段。
What you want to be doing is use an emit function inside the for loop to emit the id field of the 'track' of every artist.
然后,您要使用include_docs =查询长沙发db视图真正的参数。这是您在鸢尾花沙发上创建的数据库的最终结果。
Then you want to query couch db view with the include_docs=true parameter.Here is the final result for the database that you created on iris couch.
http://jphastings.iriscouch.com/music/_design/test/_view/nested?reduce=false&include_docs=true
{
"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在这篇文章中很好地解释了这一点
Jason explains it wonderfully in this post
此链接对于沙发数据库中的实体关系也很有帮助
this link is also helpful for entity relationships in couch db
http://wiki.apache.org/couchdb/EntityRelationship
这篇关于视图中CouchDB的链接文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!