Hey Ken, You need the phase in your application, but do you need to store it in the database, knowing that it can always be extracted from the data ?
An other solution (I think that's what Giovanni points out) is to _not_ put phases in the prescription: - When you have a new prescription, just write it to the database - To know the current phase, retrieve the 2 last doc for each prescriber (you can use your second query here). Since you already put the current prescription, this will give you the current and the last, and then you diff in your application. For each drug you know if it was there in the previous prescription or not, so you can derive phase for each drug. I'm not 100% sure, but you could even build a _reduce that creates the result for you here. Now with this you may have a problem of consistency: if one of your databases has the 2 last documents, but another database isn't perfectly synced, both databases may not give you the same output. This is kind of expected since your phases span multiple documents. If you have the last prescription id in the current prescription though, you at least know if you lack any of them (but you can't do anything without first syncing) On Fri, Apr 17, 2015 at 4:45 AM, ken tashiro <[email protected]> wrote: > Thank you again, Giovanni. > >> I don't know if I understood it correctly. Why would you need two queries >> for fetching the current prescription and the last prescription? You can do >> limit=2 and get the current and the last prescriptions at the same time. > > I am sorry for my complicated explanation. > > At the time of these two queries,current prescription data is not yet > uploaded to couchDB,so If I do limit=1 then get the last prescription data. > > If the current prescription has N drugs prescribed,first query gets at most > N "last prescriptions" that include each drug prescribed in the current > prescription regardless of who prescribed it. > So, key is [pharmacy_code,patient_code,drug_code,date], > value is phase, > > Second query gets at most one "last prescription" which is prescribed > by the same prescriber. > So, key is [pharmacy_code,patient_code,prescriber_code,date], > value is [drug_code], > > I thought these queries could be united using reduce function and different > key levels,but could not find the way. > >> For example. Then you can query this view with >> ?include_docs=true&descending=true and get all the data at once. > > By adding at most (N + 1) document ids of "the last prescription > ('last' has two meanings)" to the current prescription data which is > not yet uploaded and setting "?include_docs=true" instead of > adding "optional_information" , I want to use the content of linked > document within a map function, but it is impossible.Only client side > program which gets the query result can use linked documents? > > You indicated me to PUT the current prescription data without any > optional data, then using update functions add optional data to this > prescription data. > Am I correct? > > ken tashiro -- Matthieu RAKOTOJAONA
