Hi,
you don't have to store the intermediary results in a document - you can
have them in the view. So you're on the right track but not quite there yet.
What you can do is sth like that in the map function:
function(doc){
if(!doc.chr)return;
// define judge (for doc, NOT this)
var judge = function (no1, no2) {
var unexpect = "X";
var letter1 = unexpect, letter2 = unexpect;
for (var i in doc.chr) {
var chr = doc.chr[i];
if (chr.no == no1) {
letter1 = chr.letter;
} else if (chr.no == no2) {
letter2 = chr.letter;
}
}
if (letter1 != letter2 && letter1 != unexpect && letter2 !=
unexpect) {
return true;
}
return false;
};
// determine minimum and maximum of numbers
var minNum=doc.chr[0].no;
var maxNum=doc.chr[0].no;
for(var i=0; i<doc.chr.length; i++){
minNum=Math.min(minNum, doc.chr[i].no;
maxNum=Math.max(maxNum, doc.chr[i].no;
}
// judge for all possible combinations, and emit for judge()==true - can
be several emits per doc which is OK
for(var i=minNum; i<=maxNum; i++){
for(var j=i+1; j<=maxNum; j++){
if(judge(i, j)){
emit([i, j], null);
}
}
}
}
You can then use the key argument in the view to query for e.g. [4, 6]
Hope this helps - good luck
Sebastian
On Mon, Jan 12, 2015 at 2:57 AM, Mic <[email protected]> wrote:
> Hi Sebastian,
> Thank you for your response. In your last suggestion do you mean I would
> have to create 3 views manually, because chr.no can contain 4,5 and 6? But
> what would happen if the numbers grows?
>
> In you previous suggestion you mention another solution which would return
> duplication. In my application I could remove duplication, but
> unfortunately I do not understand your solution.
>
> Meanwhile, I am thinking whether it would be not simpler to add to each
> document all possible combinations which satisfy the judge function
> *function(doc) {*
>
>
> * var judge = function (no1, no2) { var unexpect = "X";
> var letter1 = unexpect, letter2 = unexpect; for (var i
> in this.chr) { var chr = this.chr[i]; if
> (chr.no <http://chr.no/> == no1) { letter1 =
> chr.letter; } else if (chr.no <http://chr.no/> == no2) {
> letter2 = chr.letter; } }
> if (letter1 != letter2 && letter1 != unexpect && letter2 != unexpect)
> { return true; } return false;
> };*
> * emit(judge(n1, n2), doc);*
> * }*
>
> and the result would look like this:
> {
> ...
> combinations: ['4-5', '4-6', '5-6']
> ...
> }
>
> Does JavaScript/CouchDB provide a simple way to update each document and
> create an array of all combinations?
> Is it possible to query the view for only '4-6' in combinations?
>
> Thank you in advance.
>
> Mic
>
>
>
> On Wed, Jan 7, 2015 at 6:34 AM, Sebastian Rothbucher <
> [email protected]> wrote:
>
> > Hi,
> > One more thing: you can not pass Parameters into the view ever. You have
> > to create all items in a view via emit before there is one query actually
> > running
> > What you can do, provided each document is small, is emit([no1, no2],
> > null) for all no1 and no2 the document can potentially (!!!) match
> > Again: the document is doc, not 'this'
> > Plus don't emit the full doc as 2nd Param, That Most often just slows
> > things down and is of no help. Null does it
> >
> > Good luck - regards
> > Sebastian
> >
> > Von meinem iPhone gesendet
> >
> > > Am 06.01.2015 um 13:43 schrieb Mic <[email protected]>:
> > >
> > > Hi Sebastian,
> > > Thank you for your response. For the below documents I would like only
> > get
> > > documents with the ids 7, 8 and 9, because chr.no = 5 and chr.no = 6
> > > (?key=[5,6]) only contains chr.letter between them which are not the
> same
> > > and do not have X.
> > >
> > > Threfore I thought the judge function would be useful, but maybe in
> > CouchDB
> > > is there a better way to do it.
> > >
> > > {
> > > "total_rows":5,
> > > "offset":0,
> > > "rows":[
> > > {
> > > "_id":"10",
> > > "_rev":"3-5288068d2c4ef3e6a9d3f8ff4e3377dd",
> > > "chr":[
> > > {
> > > "letter":"T",
> > > "no":4
> > > },
> > > {
> > > "letter":"A",
> > > "no":5
> > > },
> > > {
> > > "letter":"X",
> > > "no":6
> > > }
> > > ]
> > > },
> > > {
> > > "_id":"14",
> > > "_rev":"3-21300d06c31224416b8ff71b71b304d8",
> > > "chr":[
> > > {
> > > "letter":"T",
> > > "no":4
> > > },
> > > {
> > > "letter":"G",
> > > "no":5
> > > },
> > > {
> > > "letter":"G",
> > > "no":6
> > > }
> > > ]
> > > },
> > > {
> > > "_id":"7",
> > > "_rev":"2-1516ba547bdd21724158bc854f39f66b",
> > > "chr":[
> > > {
> > > "letter":"C",
> > > "no":5
> > > },
> > > {
> > > "letter":"T",
> > > "no":6
> > > }
> > > ]
> > > },
> > > {
> > > "_id":"8",
> > > "_rev":"2-750078ccc9e74616f33a2537e41b8414",
> > > "chr":[
> > > {
> > > "letter":"C",
> > > "no":5
> > > },
> > > {
> > > "letter":"T",
> > > "no":6
> > > }
> > > ]
> > > },
> > > {
> > > "_id":"9",
> > > "_rev":"2-3d68352a2d98c56fd322ae674fb7c38a",
> > > "chr":[
> > > {
> > > "letter":"A",
> > > "no":5
> > > },
> > > {
> > > "letter":"G",
> > > "no":6
> > > }
> > > ]
> > > }
> > > ]
> > > }
> > >
> > > Best wishes,
> > >
> > > Mic
> > >
> > > On Tue, Jan 6, 2015 at 4:41 AM, Sebastian Rothbucher <
> > > [email protected]> wrote:
> > >
> > >> Hi Mic,
> > >>
> > >> the general misconception here seems to be that "this" within map
> > refers to
> > >> the design doc, i.e. you'd have to have "chr" w/in the design doc -
> > which
> > >> is not the case in the example you have given.
> > >> I think it would help to explain what you want to achieve with "judge"
> > in
> > >> the first place...
> > >>
> > >> Regards
> > >> Sebastian
> > >>
> > >>> On Thu, Jan 1, 2015 at 6:06 AM, Mic <[email protected]> wrote:
> > >>>
> > >>> Hi Sinan,
> > >>> Thank you for the links and your suggestions. However, I did not
> quite
> > >>> understand your suggestions and I tried this map function which did
> not
> > >>> work:
> > >>>
> > >>> function(doc) {
> > >>> var judge = function (no1, no2) {
> > >>> var unexpect = "X";
> > >>> var letter1 = unexpect, letter2 = unexpect;
> > >>> for (var i in this.chr) {
> > >>> var chr = this.chr[i];
> > >>> if (chr.no == no1) {
> > >>> letter1 = chr.letter;
> > >>> } else if (chr.no == no2) {
> > >>> letter2 = chr.letter;
> > >>> }
> > >>> }
> > >>> if (letter1 != letter2 && letter1 != unexpect && letter2
> !=
> > >>> unexpect) {
> > >>> return true;
> > >>> }
> > >>> return false;
> > >>> };
> > >>>
> > >>>
> > >>> emit(judge(n1, n2), doc);
> > >>> }
> > >>>
> > >>> Could you please explain your ideas a little more?
> > >>>
> > >>> Thank you in advance.
> > >>>
> > >>> Mic
> > >>>
> > >>> On Wed, Dec 31, 2014 at 10:57 PM, Sinan Gabel <[email protected]
> >
> > >>> wrote:
> > >>>
> > >>>> Hi!
> > >>>>
> > >>>> You may want to take a look at the basics here:
> > >>>> http://docs.couchdb.org/en/latest/couchapp/views/index.html and
> > >> Cloudant
> > >>>> has some interactive samples at:
> > >>>> https://cloudant.com/for-developers/all_docs/
> > >>>>
> > >>>> *Using map*: You may want to emit the letters and use keys to find
> the
> > >>>> docs.
> > >>>>
> > >>>> *Not using map*: Name each doc something "smart", and use keys to
> find
> > >>> the
> > >>>> docs.
> > >>>>
> > >>>> Br,
> > >>>> Sinan
> > >>>>
> > >>>>> On 31 December 2014 at 13:05, Mic <[email protected]> wrote:
> > >>>>>
> > >>>>> Hi,
> > >>>>> I tried to create a map function which should use the following
> > >>>> conditions:
> > >>>>>
> > >>>>> - e.g. chr.no = 5 and chr.no = 6
> > >>>>> - chr.letter between two objects/dicts in chr are not the same and
> > >> no
> > >>> X
> > >>>>>
> > >>>>> I have the following function but I do not know how to combine it
> > >> with
> > >>>>> `emit`
> > >>>>>
> > >>>>> var judge = function (no1, no2) {
> > >>>>> var unexpect = "X";
> > >>>>> var letter1 = unexpect, letter2 = unexpect;
> > >>>>> for (var i in this.chr) {
> > >>>>> var chr = this.chr[i];
> > >>>>> if (chr.no == no1) {
> > >>>>> letter1 = chr.letter;
> > >>>>> } else if (chr.no == no2) {
> > >>>>> letter2 = chr.letter;
> > >>>>> }
> > >>>>> }
> > >>>>> if (letter1 != letter2 && letter1 != unexpect && letter2 !=
> > >>>>> unexpect) {
> > >>>>> return true;
> > >>>>> }
> > >>>>> return false;
> > >>>>> };
> > >>>>>
> > >>>>>
> > >>>>> CouchDB has the below content:
> > >>>>>
> > >>>>>
> > >>>>> {
> > >>>>> "total_rows":5,
> > >>>>> "offset":0,
> > >>>>> "rows":[
> > >>>>> {
> > >>>>> "_id":"10",
> > >>>>> "_rev":"3-5288068d2c4ef3e6a9d3f8ff4e3377dd",
> > >>>>> "chr":[
> > >>>>> {
> > >>>>> "letter":"T",
> > >>>>> "no":4
> > >>>>> },
> > >>>>> {
> > >>>>> "letter":"A",
> > >>>>> "no":5
> > >>>>> },
> > >>>>> {
> > >>>>> "letter":"X",
> > >>>>> "no":6
> > >>>>> }
> > >>>>> ]
> > >>>>> },
> > >>>>> {
> > >>>>> "_id":"14",
> > >>>>> "_rev":"3-21300d06c31224416b8ff71b71b304d8",
> > >>>>> "chr":[
> > >>>>> {
> > >>>>> "letter":"T",
> > >>>>> "no":4
> > >>>>> },
> > >>>>> {
> > >>>>> "letter":"G",
> > >>>>> "no":5
> > >>>>> },
> > >>>>> {
> > >>>>> "letter":"G",
> > >>>>> "no":6
> > >>>>> }
> > >>>>> ]
> > >>>>> },
> > >>>>> {
> > >>>>> "_id":"7",
> > >>>>> "_rev":"2-1516ba547bdd21724158bc854f39f66b",
> > >>>>> "chr":[
> > >>>>> {
> > >>>>> "letter":"C",
> > >>>>> "no":5
> > >>>>> },
> > >>>>> {
> > >>>>> "letter":"T",
> > >>>>> "no":6
> > >>>>> }
> > >>>>> ]
> > >>>>> },
> > >>>>> {
> > >>>>> "_id":"8",
> > >>>>> "_rev":"2-750078ccc9e74616f33a2537e41b8414",
> > >>>>> "chr":[
> > >>>>> {
> > >>>>> "letter":"C",
> > >>>>> "no":5
> > >>>>> },
> > >>>>> {
> > >>>>> "letter":"T",
> > >>>>> "no":6
> > >>>>> }
> > >>>>> ]
> > >>>>> },
> > >>>>> {
> > >>>>> "_id":"9",
> > >>>>> "_rev":"2-3d68352a2d98c56fd322ae674fb7c38a",
> > >>>>> "chr":[
> > >>>>> {
> > >>>>> "letter":"A",
> > >>>>> "no":5
> > >>>>> },
> > >>>>> {
> > >>>>> "letter":"G",
> > >>>>> "no":6
> > >>>>> }
> > >>>>> ]
> > >>>>> }
> > >>>>> ]
> > >>>>> }
> > >>>>>
> > >>>>> How is it possible to write a map function combine with the above
> > >>> `judge`
> > >>>>> function or maybe there is better way to do it?
> > >>>>>
> > >>>>> P.S. I also asked the same question here (
> > >>
> >
> http://stackoverflow.com/questions/27688196/map-function-with-a-helper-function-in-couchdb
> > >>>>> )
> > >>>>>
> > >>>>> Thank you in advance.
> > >>>>>
> > >>>>> Mic
> > >>
> >
>