CouchDB “Join” two documents

前端 未结 1 917
耶瑟儿~
耶瑟儿~ 2021-02-05 16:46

I have two documents that looks a bit like so:

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

DataKey
{
  _id: ...,
  credits_left: 500,
  times_used: 0,
           


        
相关标签:
1条回答
  • 2021-02-05 17:06

    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.

    0 讨论(0)
提交回复
热议问题