Sorry, but I still don't get what are the application requirements.
Each prescription is a document, right? Each prescription document has
{
"prescriber": "the id of the prescriber",
"patient": "the id of the patient",
"date": "the date",
"drugs": [
"an array of the ids",
"of all the drugs",
"in this prescription"
]
}
Is that right?
You need the phase of each drug, which is only a number that is
1 if drug is in the last prescription, but not in the second last;
2 if drug is in the the last and second last prescriptions;
3 if drug is in the last prescription but not in the second last.
Is that right?
But you also need the last prescription done by the same prescriber who is
consulting the database at the moment, regardless of it being the last
prescription for that patient or not?
What is stopping you from getting all the previous (or, say, the last 20
previous) prescriptions for each patient in the same query and
showing/using/calculating based on that data?
Also important: it is impossible to link documents, each document is an
island. It can be broken into various different keys in a view (it can emit
various keys from one view), but it cannot be joined or directly referenced
by another. Update functions act on individual documents, I only
recommended using those when I suggested that you kept only one document
for each patient with all its prescriptions inside.
Again, sorry for my inability to comprehend.
On Fri, Apr 17, 2015 at 6:19 AM, rara7020 . <[email protected]>
wrote:
> 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
>