2

Say I have two collections like this:

// City Collection
<city>
  <id>1</id>
  <name>Tulsa</name>
  <stateId>1></stateId>
</city>

// State Collection
<state>
  <id>1</id>
  <name>Oklahoma</name>
</state>

Now say I want to return a list of the cities with the state name instead of stateId. How can I do this join efficiently, using the node.js client API and the query builder?

4
  • 1
    I'm not sure you can do it efficiently from there. It's possible to do this with a scatter query in XQuery (or server-side JS in v8.0), but with Node I think you'd end up performing two queries in sequence. The recommended way to do this is to denormalize your content so that every city document contains the name of the state it's in. Commented May 29, 2015 at 18:43
  • So we are on MarkLogic 8 actually. Would this be something I can do with the server side javascript thing? I may actually have them just put the names in the city collection with the state id's that would make my life a lot simpler. Commented May 29, 2015 at 18:47
  • 1
    Exactly that -- the best approach for a document database differs from a relational database in denormalizing to reduce the need for joins to cases where independent entities have frequently modified properties. In those cases, as Will suggests, the optimal approach is to write a server-side extension to implement the join. Commented May 29, 2015 at 19:14
  • It would be good for wst or Erik to add the two approaches as an answer so that this question can be considered "done". Commented May 29, 2015 at 20:58

1 Answer 1

2

An efficient join can't be performed using the Node.js API. The best way to approach this, if possible, is to denormalize your content so that documents contain any information you need to query or return.

<city>
  <id>1</id>
  <name>Tulsa</name>
  <stateId>1></stateId>
  <state-name>Oklahoma</state-name>
</city>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.