Dear Couchbase Colleges,
I got stuck in a problem and I’m not sure I’m handling it property.
In my relational database I have PRODUCTS, COMPATIBILITY, DEVICES, PRODUCT_GROUP and REGION. Products has a relation N:M to Devices using Compatibility table to make the association. And each product can be sold in one or more region being associated using PRODUCT_GROUP (informing the group in which the product will be placed in that region). So I designed my documents like:
{ type:“PRODUCT”,
docId:“pd1”,
name:“Product Name”,
groups: {“rgid1”:“2013”,“rgid1”:“Comedy” …},
compatibility: [“cp1”,“cp3”]}
{ type:“COMPATIBILITY”,
docId:“cp1”,
name:“Compatibility Name”,
provisioningKey:“XY012301CCD”,
devices: [“dv1”, “dv3”]}
This would be my first idea, I just separated the compatibility record because have a property (provisioningKey) that is specific for each compatibility record and changes each 2 days, this would then avoid to update all documents with the new key. My map function with this approach was something like:
function (doc, meta) {
for (i in doc.groups) {
for (j in doc.region[i].compatibility) {
emit([doc.region[i].compatibility[j], i, doc.groups[i]], 1);
}
}
}
And using _count reduce function. The number of devices associated to compatibility records is huge, and the idea was to fetch all compatibility records associated to the device and use it to fetch all compatible products using this view, for example: I retrieve from another view that Device dv03 is assocated to cp1 and cp3, in a second step I use it to fetch all products from the given region for groups from A to B that have cp1 or cp3 in the complex key. Is this possible? Is there a simple/better way to solve this?
My second approach would be to integrate the device with my product like this:
{ type:“PRODUCT”,
docId:“pd1”,
name:“Product Name”,
groups: {“rgid1”:“2013”,“rgid1”:“Comedy”},
devices: [“dv1”, “dv2”, “dv3”]}
{ type:“COMPATIBILITY”,
docId:“cp1”,
name:“Compatibility Name”,
provisioningKey:“XY012301CCD”}
And the map-function like:
function (doc, meta) {
for (i in doc.groups) {
for (j in doc.region[i].devices) {
emit([doc.region[i].devices[j], i, doc.groups[i]], 1);
}
}
}
This is ok for a few hundred of products. Each product is associated at the end with more than 3000 devices, and the indexing time will take too much time when importing new bunch of products to the database. The import of new products happens at least twice a day. Considering this I would think the first approach is better since new products are indexed easily by using compatibility keys instead of each device associated to the compatibility record.
Does someone faced the same problem? In this case what was the solution? Or maybe someone has a better solution/idea for the problem I exposed?