0
votes

Noob Question on Couchbase / NOSQL :

I have 2 different types of documents which have a common field across them, I would like to join them using views based on that common field :

Document 1 - Key : Page_Object1

{
  "URL": "/someurl/",
  "title": "some title"  
}

Document 2 Key : Zone_Object1

{
  "URL": "/someurl/",
  "zone": "some Ad zone"
  "
}

Can someone pls help me join these 2 documents and return a single joined document (using view?) based off of the "url" field, both these documents live in the same bucket.

2

2 Answers

2
votes

Actually, I guess the view code posted will not work.

The correct would be:

if (doc.title) {
   emit (doc.url, doc.title)
} else if (doc.zone) {
   emit (doc.url, doc.zone)
}

And it's true,

If you have N1QL available, you'll get a proper join mechanism.

0
votes

According to the Couchbase documentation http://docs.couchbase.com/admin/admin/Views/views-querySample.html, this is not possible in the strict sense.

But you can define your view like

if (doc.type == "page") {
   emit (doc.url, doc.title)
} else if (doc.type == "zone") {
   emit (doc.url, doc.zone)
}

Now, when querying this view for a given url, you'll get both informations.

If you have N1QL available, you'll get a proper join mechanism.