How does Simulating Joins works in Couchbase?

后端 未结 2 768
粉色の甜心
粉色の甜心 2021-01-13 20:01

I have documents one is dependent to other. first:

{
  \"doctype\": \"closed_auctions\",
  \"seller\": {
    \"person\": \"person11304\"
  },
  \"buyer\":          


        
2条回答
  •  有刺的猬
    2021-01-13 20:31

    An other solution consist to merge datas in a custom reduce function.

    // view
    function (doc, meta) {
      if (doc.doctype === "people") {
         emit(doc.id, doc);
      }
      if (doc.doctype === "closed_auctions") {
         emit(doc.buyer.person, doc);
      }
    }
    
    // custom reduce
    function (keys, values, rereduce) {
       var peoples = values.filter(function (doc) {
           return doc.doctype === "people";
       });
       for (var key in peoples) {
          var people = peoples[key];
          people.closed_auctions = (function (peopleId) {
              return values.filter(function (doc) {
                 return doc.doctype === "closed_auctions" && doc.buyer.person === peopleId;
              });
          })(people.id);
       }
       return peoples;
    }
    

    And then you can query one user with "key" or multiple users with "keys".

    After I don't know what the performances issues are with this method.

提交回复
热议问题