4D v18

collection.query( )

Home

 
4D v18
collection.query( )

collection.query( )  


 

The collection.query( ) method returns all elements of a collection of objects that match the search conditions defined by queryString and (optionally) value and querySettings. If the original collection is a shared collection, the returned collection is also a shared collection.

Note: This method does not modify the original collection.

The queryString parameter uses the following syntax:

propertyPath comparator value {logicalOperator propertyPath comparator value}

For detailed information on how to build a query using queryStringvalue and querySettings parameters, please refer to the dataClass.query( ) method description.

 C_COLLECTION($c)
 $c:=New collection
 $c.push(New object("name";"Cleveland";"zc";35049))
 $c.push(New object("name";"Blountsville";"zc";35031))
 $c.push(New object("name";"Adger";"zc";35006))
 $c.push(New object("name";"Clanton";"zc";35046))
 $c.push(New object("name";"Clanton";"zc";35045))
 $c2:=$c.query("name = :1";"Cleveland") //$c2=[{name:Cleveland,zc:35049}]
 $c3:=$c.query("zc > 35040") //$c3=[{name:Cleveland,zc:35049},{name:Clanton,zc:35046},{name:Clanton,zc:35045}]

 C_COLLECTION($c)
 $c:=New collection
 $c.push(New object("name";"Smith";"dateHired";!22-05-2002!;"age";45))
 $c.push(New object("name";"Wesson";"dateHired";!30-11-2017!))
 $c.push(New object("name";"Winch";"dateHired";!16-05-2018!;"age";36))
 $c.push(New object("name";"Sterling";"dateHired";!10-5-1999!;"age";Null))
 $c.push(New object("name";"Mark";"dateHired";!01-01-2002!))

This example returns persons whose name contains "in":

 $col:=$c.query("name = :1";"@in@")
  //$col=[{name:Winch...},{name:Sterling...}]

This example returns persons whose name does not begin with a string from a variable (entered by the user, for example):

 $col:=$c.query("name # :1";$aString+"@")
  //if $astring="W"
  //$col=[{name:Smith...},{name:Sterling...},{name:Mark...}]

This example returns persons whose age is not known (property set to null or undefined):

 $col:=$c.query("age=null") //placeholders not allowed with "null"
  //$col=[{name:Wesson...},{name:Sterling...},{name:Mark...}]

This example returns persons hired more than 90 days ago:

 $col:=$c.query("dateHired < :1";(Current date-90))
  //$col=[{name:Smith...},{name:Sterling...},{name:Mark...}] if today is 01/10/2018

Note: This last example requires that the "Use date type instead of ISO date format in objects" compatibility option is checked (see Compatibility page).

More examples of queries can be found in the dataClass.query( ) page. 



See also 

collection.indices( )
dataClass.query( )
entitySelection.query( )

 
PROPERTIES 

Product: 4D
Theme: Collections

This command can be run in preemptive processes

 
PAGE CONTENTS 
 
HISTORY 

 
ARTICLE USAGE

4D Language Reference ( 4D v18)