KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Querying OBJECT Fields with QUERY BY ATTRIBUTE
PRODUCT: 4D | VERSION: 15.1 | PLATFORM: Mac & Win
Published On: February 3, 2016

It is possible to query Object type fields in v15. Querying Objects is limited to only code as of v15 and cannot be done in the query editor. To query Object fields the command QUERY BY ATTRIBUTES is used.



This increases the usefulness of using Object type fields. This function is very similar the the QUERY commands, the major point is the attributePath parameter. The parameter is used to specify which attribute of each records' object to compare the value against. The parameter can contain as many levels of objects as needed and can even handle arrays.

Examples:
To query and search for a single attrribute the command would resemble:

Sample Objects' structure in some records' Object fields:
(
   "Attribute" : "Value"
}

QUERY BY ATTRIBUTE([Table];[Table]ObjectField;"Attribute";=;$Var)

If an attribute in an object contains another object list the attributes in the parameter using a dot notation:
Sample Objects' structure in some records' Object fields:
(
   "Level1Attr" : {
                  "Level2Attr1" : "Value",
                  "Level2Attr2" : {
                                 "Level3Attr" : "Value"
                                 }
                  }
}

//Querying 2 level depth
QUERY BY ATTRIBUTE([Table];[Table]ObFld;"Lvl1Attr.Lvl2Attr1";=;$Var1)
//Querying 3 level depth
QUERY BY ATTRIBUTE([Table];[Table]ObFld;"Lvl1Attr.Lvl2Attr2.Lvl3Attr";=;$Var2)


If an attribute contains an object array, append square brackets at the end of the array attribute in the parameter:
Sample Objects' structure in some records' Object fields:
(
   "ArrayAttribute" : [ {
                     "ArrAttribute1" : "Arr1Value1",
                     "ArrAttribute2" : "Arr1Value2"
                  } , {
                     "ArrAttribute1" : "Arr2Value1",
                     "ArrAttribute2" : "Arr2Value2"
                  } ]
}

QUERY BY ATTRIBUTE([Table];[Table]ObFld;"ArrayAttribue[].ArrAttribute1";=;$Var)


An important note is that the # query operator, in the use of this command, will also return a match if the attribute does not exist.

This command can also be mixed with the QUERY commands when building multiple querys:
QUERY([Table1];[Table1]Field1;=;$var1;*)
QUERY BY ATTRIBUTE([Table1];[Table1]Field2;"Attribute";=;$var2;*)
QUERY([Table1];[Table1]Field3;=;$var3)