CouchDB links multiple documents

Is it possible to link multiple documents in one view.

For instance:

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

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

View:

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

In the request, I use includeocs = true.

How can I emit several documents corresponding to several keys in one document?

+4
source share
1 answer

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

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

The Couch db wiki lists another way to do this by iterating through an array and passing related documents one at a time.

+5
source

Source: https://habr.com/ru/post/1543379/


All Articles