4
votes

Is it possible to link multiple documents in one view.

Eg :

{
  "_id" : "0b86008d8490abf0b7e4f15f0c6a463b",
  "name" : "copenhagen"}
{
  "_id" : "8986008d8490abf0b7e4f15f0c6a333b",
  "player" : "Mark"
}
{
  "_id" : "4b86008d8490abf0b7e4f15f0c6a463c",
  "location" : { "uuid" : "0b86008d8490abf0b7e4f15f0c6a463b"},
  "player" : { "uuid" : "8986008d8490abf0b7e4f15f0c6a333b"},
  "session" : "9876"
}

I want a view to include location document as well as the player document.

View :

  "fetchByLocationAndPlayer": {
       "map": "function(doc) {    if (doc.session) {  emit(doc.session, { _id : **doc.location.uuid** });     } }"
   }

In the query I use includedocs = true.

How do I emit multiple documents corresponding to multiple keys in one document?

1

1 Answers

5
votes

Yes it is possible. Just use two emits instead of one

emit(doc.session, {_id:doc.location.uuid});
emit(doc.session,{_id:doc.player.uuid});

Couch db wiki lists yet another way of doing this by iterating over the array and emitting linked docs one by one.