How does Simulating Joins work in Couchbase?

I have documents that depend on each other. first:

{
  "doctype": "closed_auctions",
  "seller": {
    "person": "person11304"
  },
  "buyer": {
    "person": "person0"
  },
  "itemref": {
    "item": "item1"
  },
  "price": 50.03,
  "date": "11/17/2001",
  "quantity": 1,
  "type": "Featured",
  "annotation": {
    "author": {
      "person": "person8597"
    }
}

here you can see that doc.buyer.person depends on other documents like this:

{
  "doctype": "people",
  "id": "person0",
  "name": "Kasidit Treweek",
  "profile": {
    "income": 20186.59,
    "interest": [
      {
        "category": "category251"
      }
    ],
    "education": "Graduate School",
    "business": "No"
  },
  "watch": [
    {
      "open_auction": "open_auction8747"
    }
  ]
}

How can I get the name of the buyer from these two documents? I mean doc.buyer.person is associated with a second document id. This connection is also not clear from the documentation. http://docs.couchbase.com/couchbase-manual-2.0/#solutions-for-simulating-joins

+4
source share
2 answers

Well, firstly, let me point out that the very first sentence of the documentation section that you are referencing says (I added emphasis):

, , , .

, , . :

  • , . , PersonId , , .
  • , , . # 1 linq.
  • , , . Person .
  • , , . , , Person. , .

, . .

+5

.

// 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;
}

"" "".

, , .

+2

All Articles