If, on the other hand, they are not one to one, but with the help of a complex key, you can use view matching for the connection.
function(doc) { if (doc.type == 'order') { emit([doc.cartid, 1], doc); } else if (doc.type == 'cart') { emit([doc.id, 0], doc); } }
documents will be matched by potatoes with orders received after the cart. Application code can easily join this stream, and you can request a specific cartridge using the start key and endpoint.
see Viewing Sort Order for Sort Rules.
You can also use a shortcut to combine them together.
Just change the map function to this:
function(doc) { if (doc.type == 'order') { emit([doc.cartid, 1], {cartid: doc.cartid, orders: [doc]}); } else if (doc.type == 'cart') { emit([doc.id, 0], {cartid: doc.id, orders: [], cart: doc); } }
and add a reduction function, for example:
function(keys, values) { var out = {cartid: null, orders: [], cart: null}; for (idx in values) { var doc = values[idx]; out['cartid'] = doc.cartid; if (doc.cart) { out['cart'] = doc.cart }; for (idx2 in doc.orders) { out.orders.push(doc.orders[idx2]); } } return out; }
This will lead to the return of one document to the basket, which will indicate the potatoes, an array of order documents and a basket document.
Sorry if there are errors in the above code, but I don't have a test couchdb instance that I can try. You should get a general idea, although the CouchDB wiki has more detailed information.