0
votes

I've started using N1ql and I have been following the N1ql tutorials from Couchbase.

I have noticed many times that a given document (let's say User) contains an array with the ids of other documents (let's say orders), from which we can later on perform a join. Is this solution suitable for a situation where a user might have thousands of references?

If not, what is the alternative? What would be the correct approach to find all users and their orders (where some users may not have orders)?

1

1 Answers

2
votes

yes, it should be suitable, as long as other due diligence/sizing is taken care. As usual, the data model and associated queries should be considered together. For example, if queries are based on specific array elements, you can use array indexes.

Alternatives can be (depending on various factors for data modeling):

  1. embed other documents (denormalizing), or
  2. reverse referencing, where order-documents have a reference to the corresponding user-document. Note that, from Couchbase 4.5 release, N1QL supports Lookup JOINs & Index JOINs that helps JOINing users to orders, irrespective of whether USERs refer ORDERs or vice-versa. http://developer.couchbase.com/documentation/server/4.5/n1ql/n1ql-language-reference/from.html#story-h2-3

-Prasad