couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Randall Leeds <>
Subject Re: business model / 'ER' help
Date Tue, 01 Jun 2010 03:39:54 GMT
The easiest thing in my mind would be to have a document for each
inventory item that contains its name and the store where it's in
This is actually kinda nice because it lets you track individual
instances of each item, like if you wanted to order the item to be
sent from one store to another you could modify the store property.

I think you can get away with only two map functions.
Make one map that emits a key like {[item, store]: item_id}. Call it
Another map emits keys like {[store, item]: item_id}. Call it "items_by_store".
Then all three of your questions are simple summation ({"reduce":
"_count"}) reductions.

1. _design/inventory/_view/items_by_name?group=true&group_level=1
2. _design/inventory/_view/items_by_name?group=true&group_level=2&startkey="[item_name]"&endkey="[item_name,
3. _design/inventory/_view/items_by_store?group=true&group_level=2&startkey="[store_name]"&endkey="[store_name,

I think this would work for you. You might need to url encode those keys.
The startkey and endkey parameters are chosen based on the collation

Happy map(-reduc)ing!


On Mon, May 31, 2010 at 20:14,  <> wrote:
> Hi I am trying to get my head around a simple practice task, however I
> can't stop thinking about relational models as opposed to document models
> hehe
> ok so i have one couchDB database that has to contain 3 stores, each store
> has a product inventory (i.e product name, qty)
> I have to be able to:
> 1. list the total qty of each item (i.e across all stores)
> 2. list the QTY of a certain item within each store
> 3. list all items, their qty for a specific store
> ...I could smash this out in SQL in a few minutes...but cant get my head
> around doing it as would I have a document for each store
> containing product attributes(i.e p1, p2 etc)? or a document for each
> product containing store attributes(i.e P > s1-qty, s2-qty, s3-qty)?
> or (and i doubt this be true) would i have separate store docs and product
> docs and somehow link then like I would in a 'real' (:-p) database lol
> wasnt sure if these kinds of Q's were asked in here so thought id just
> give it a shot

View raw message