CouchDB “Join” two documents

2019-03-15 06:36发布

问题:

I have two documents that looks a bit like so:

Doc
{
  _id: AAA,
  creator_id: ...,
  data: ...
}

DataKey
{
  _id: ...,
  credits_left: 500,
  times_used: 0,
  data_id: AAA
}

What I want to do is create a view which would allow me to pass the DataKey id (key=DataKey _id) and get both the information of the DataKey and the Doc.

My attempt:

I first tried embedding the DataKey inside the Doc and used a map function like so:

function (doc)
{
  if (doc.type == "Doc")
  {
    var ids = [];
    for (var i in doc.keys)
      ids.push(doc.keys[i]._id);

    emit(ids, doc);
  }
}

But i ran into two problems:

  1. There can be multiple DataKey's per Doc so using startkey=[idhere...] and endkey=[idhere..., {}] didn't work (only worked if the key happend to be the first one in the array).
  2. All the data keys need to be unique, and I would prefer not making a seperate document like {_id = datakey} to reserve the key.

Does anyone have ideas how I can accomplish this? Let me know if anything is unclear.

-----EDIT-----

I forgot to mention that in my application I do not know what the Doc ID is, so I need to be able to search on the DataKey's ID.

回答1:

I think what you want is

function (doc)
{
  if (doc.type == "Doc")
  {
    emit([doc._id, 0], doc);
  }

  if(doc.type == "DataKey")
  {
    emit([doc.data_id, 1], doc);
  }
}

Now, query the view with key=["AAA"] and you will see a list of all docs. The first one will be the real "Doc" document. All the rest will be "DataKey" documents which reference the first doc.

This is a common technique, called CouchDB view collation.