JP.
JP.

Reputation: 5594

CouchDB's Linked Documents in a View

I'm having a hard time getting my head around CouchDB's linked documents feature.

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);
  }
}

example here: 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

Upvotes: 4

Views: 5323

Answers (1)

Akshat Jiwan Sharma
Akshat Jiwan Sharma

Reputation: 16050

Okay I finally understand what you are trying to do.Yes this is possible.Here is how.

You have 2 documents

{
"_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)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 });
        }
    }
}

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.

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 explains it wonderfully in this post

Best way to do one-to-many "JOIN" in CouchDB

this link is also helpful for entity relationships in couch db

http://wiki.apache.org/couchdb/EntityRelationship

Upvotes: 5

Related Questions