javascripttaffydb

taffydb using OR and AND in single query


My TAFFYDB() has rows that look like this (for example):

[
{"rowID":0,"objID":"hema","objVal":1,"objType":"n"},
{"rowID":0,"objID":"osmo","objVal":1,"objType":"n"},
{"rowID":0,"objID":"mot","objVal":1,"objType":"n"},
{"rowID":0,"objID":"morph","objVal":1,"objType":"n"},
{"rowID":0,"objID":"ph","objVal":0,"objType":"n"},
{"rowID":0,"objID":"conc","objVal":0,"objType":"n"}
]

I want to query out on 2 conditions:

  1. morph or mot must have the value of 1
  2. ph and osmo must have the value of 0

for matching rowID's

I'm trying like this:

var ret=anaDataDB(
            [ 
                [ [{"objID":"morph","objID":"mot"}] ,{ "objVal":1} ] ,
                [ [{"objID":"osmo"} ] ,{ "objVal":0 } ],
                [ [{"objID":"ph"} ] ,{ "objVal":0 } ]
            ]       
            ).get();

but I'm getting too many values. For instance, values where ph=1. I think the way I have it is using OR and I want AND between the 3 comparisons.

How do I do that?


Solution

  • TaffyDB's query system is a bit awkward until you get used to it.

    The simple trick to it is this: The elements within an array are considered part of an OR condition. Matching is attempted on objects. If the object in the parameter fits the record you are looking for, it will be returned.

    var ans = anaDataDB(
        [
            {objID:["morph", "mot"], objVal:1},
            {objID:["osmo", "ph"], objVal:0}
        ]
        ).get();
    

    This returns:

    [ { rowID: 0,
        objID: 'mot',
        objVal: 1,
        objType: 'n',
        ___id: 'T000002R000004',
        ___s: true },
      { rowID: 0,
        objID: 'morph',
        objVal: 1,
        objType: 'n',
        ___id: 'T000002R000005',
        ___s: true },
      { rowID: 0,
        objID: 'ph',
        objVal: 0,
        objType: 'n',
        ___id: 'T000002R000006',
        ___s: true } ]
    

    You're saying:

    "I want the record if it has an objID of "morph" OR "mot" where the objVal is 1, OR if the record has an objID of "osmo" OR "ph" where the objVal is 0."

    Objects are for matching, arrays make an OR.