6

Tech being used:

  • Node.js
  • Nano
  • CouchDB

Disclaimer:

I am very new to CouchDB. I am able to query views with Node.js and Nano just fine. I am looking for more of a conceptual solution then a syntax solution.

Scenario:

I am building a very basic web app. I am attempting to get a baseball player from the database and place all his data into a player/profile page.

This player is going to belong to a team. The team has a short name and a long name.

Here are my databases:

{ name: "Derek Jeter", homeTown: "Brooklyn, New York", team_id: 10 } // player
{ shortName: "Yankees", longName: "NY Yankees", team_id: 10 } // team

I am trying to return a JSON object that looks like this:

{ name: "Derek Jeter", shortName: "Yankees" }

I know that I can include everything into a single database and use the "type" property to emit() based on the type, but I really don't want to do that.

Question:

How can I, using Node and Nano to query the player for Derek Jeter and join the other data from the other database? I know a view doesn't have access anything other then the current document that it resides, but there has to be another way?

I have searched multiple time on Stackoverflow and Google and I haven't found anyone straying away from the "type" solution.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Phil
  • 10,948
  • 17
  • 69
  • 101
  • 2
    If you're using node, your only real option is to query both databases and do your merge/join in memory. If you ever need to join across databases, you really really should consider combining the 2. – Dominic Barnes Jan 17 '15 at 01:00
  • Hey @DominicBarnes, but aren't I forced to query twice anyway? How can I get the contents of `team_id` AND the player in ONE query? – Phil Jan 19 '15 at 14:43
  • 2
    There's actually a wonderful feature of CouchDB called "[Joined Documents](http://docs.couchdb.org/en/1.6.1/couchapp/views/joins.html?highlight=join)". The gist is, emit an object that looks like `{ _id: "related-doc-uuid" }` and when you use `include_docs=true` in your view query, it will pull *that document* instead of the original one. – Dominic Barnes Jan 19 '15 at 16:02
  • 4
    And just to be clear, the "Joined Documents" feature **only** works on documents within the *same* database. – Dominic Barnes Jan 19 '15 at 16:08
  • Thank you! Just out of curiosity, the tutorials I see for joins require the "related-doc-uuid" to correspond to the to-be-joined document's _id, can I match it to a property instead? (I have tried it and it doesn't seem to work) – Phil Jan 19 '15 at 16:23
  • 1
    Nope, only the `_id` can be used, it's the only way it can narrow down to a single document reliably and quickly. – Dominic Barnes Jan 19 '15 at 18:54
  • Thanks again. Also, I really am having a tough time finding resources on CouchDB. I have tons of beginner questions that I can't seem to get answered. Do you have a tutorial do you give tutorials? – Phil Jan 19 '15 at 20:33
  • 1
    Their [core docs](http://docs.couchdb.org/en/1.6.1/) are really quite good, so I would start there. – Dominic Barnes Jan 22 '15 at 22:20

0 Answers0