couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Simon Metson <simonmet...@googlemail.com>
Subject Re: VERY Basic Question
Date Wed, 15 Feb 2012 15:12:20 GMT
Hi Lauren, 


On Wednesday, 15 February 2012 at 14:49, Lauren Dahlin wrote:

> Also, I am working on a very large database so testing is difficult. 
> How can I sample (doesn't have to be random) a given number of 
> documents?
That's currently not done easily. You'd need to hit _all_docs with a limit parameter (e.g.
http://localhost:5984/demo/_all_docs?limit=100) then push the result back into your subset
database. Give me some time and I'll knock up a python script to do it...
> > First, does anyone have any very simple resources for learning how 
> > to use views? Coming from a limited SQL background, I'm so confused. 
> > What I am really having trouble with is how to query subvalues. 
> > (Also, is "subvalue" the right word?) For example, say I have:
> > { "_id" : "bc2a41170621c326ec68382f846d5764", "_rev" : "2612672603", 
> > "item" : "apple", "prices" : { "Fresh Mart" : 1.59, "Price Max" : 
> > 5.99, "Apples Express" : 0.79 } }
> > (example from http://answers.oreilly.com/topic/1396-running-a-couchdb-query-using-mapreduce/)
> > How would I only return the price associated with "Fresh Mart" for 
> > every ID containing a Freshmart price?
> > 
> > 
> 
> 
> 


You need to write a map reduce view, then query it appropriately. The following will return
a row for every price, keyed on the store:

function(doc) {
  if (doc.prices){
    for (store in doc.prices){
      emit(store, doc.prices[store]);
    }
  }
}


That'll give you back data like:

{"total_rows":6,"offset":0,"rows":[ {"id":"6152bfe666b48bf57063b055120003df","key":"Apples
Express","value":0.79}, {"id":"6152bfe666b48bf57063b05512000c88","key":"Bananas 4 U","value":1.79},
{"id":"6152bfe666b48bf57063b055120003df","key":"Fresh Mart","value":1.59}, {"id":"6152bfe666b48bf57063b05512000c88","key":"Fresh
Mart","value":2.79}, {"id":"6152bfe666b48bf57063b055120003df","key":"Price Max","value":5.99},
{"id":"6152bfe666b48bf57063b05512000c88","key":"Price Max","value":3.99} ]}

You might want to add the item into the views key, so you can query for the price of apples
at Fresh Mart:

function(doc) {
  if (doc.prices){
    for (store in doc.prices){
      emit([doc.item, store], doc.prices[store]);
    }
  }
}


That returns:
{"total_rows":6,"offset":0,"rows":[ {"id":"6152bfe666b48bf57063b055120003df","key":["apple","Apples
Express"],"value":0.79}, {"id":"6152bfe666b48bf57063b055120003df","key":["apple","Fresh Mart"],"value":1.59},
{"id":"6152bfe666b48bf57063b055120003df","key":["apple","Price Max"],"value":5.99}, {"id":"6152bfe666b48bf57063b05512000c88","key":["banana","Bananas
4 U"],"value":1.79}, {"id":"6152bfe666b48bf57063b05512000c88","key":["banana","Fresh Mart"],"value":2.79},
{"id":"6152bfe666b48bf57063b05512000c88","key":["banana","Price Max"],"value":3.99} ]}
I can query it like http://localhost:5984/demo/_design/demo/_view/items?startkey=[%22apple%22]&endkey=[%22apple%22,%20{}]
(http://localhost:5984/demo/_design/demo/_view/items?startkey=%5B%22apple%22%5D&endkey=%5B%22apple%22,%20%7B%7D%5D)
to get just apples:
{"total_rows":6,"offset":0,"rows":[ {"id":"6152bfe666b48bf57063b055120003df","key":["apple","Apples
Express"],"value":0.79}, {"id":"6152bfe666b48bf57063b055120003df","key":["apple","Fresh Mart"],"value":1.59},
{"id":"6152bfe666b48bf57063b055120003df","key":["apple","Price Max"],"value":5.99} ]}
Or like http://localhost:5984/demo/_design/demo/_view/items?key=["apple","Fresh Mart"] (http://localhost:5984/demo/_design/demo/_view/items?key=%5B%22apple%22,%22Fresh%20Mart%22%5D)
to get just Fresh Mart's prices:
{"total_rows":6,"offset":1,"rows":[ {"id":"6152bfe666b48bf57063b055120003df","key":["apple","Fresh
Mart"],"value":1.59} ]}

Hope that helps
Simon



Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message