Query collections

Simplicity is the ultimate sophistication, and with over 40 methods in 4D v16 R6 to manage collections, things are getting much easier for you as a developer. Not only is your code clean and more readable, but its also written in record time!

In this blog post we’ll take a look at how the two new methods, query and indices, will allow you to manipulate and retrieve items from a collection in a snap!


The query and indices methods, as in a traditional 4D query, are used to manipulate and retrieve items in a collection. They work exactly the same, the only difference is in the returned result: query returns a collection containing all of the objects matching the search condition defined by the query argument, while indices returns a collection of indexes.

Database example: query collections

You can mix two types of operators to query your collection. The following code samples show you what’s possible and how to proceed.

How to write a query?

Starter example

There are two possible ways to write a query:

  • query as a string:

$c:=New collection(New object("Id";128;"City";"Paris";"Weather";1))
$c.push(New object("Id";50;"City";"New-York";"Weather";-10))
$c.push(New object("Id";1792;"City";"Tokyo";"Weather";-5))
$c.push(New object("Id";2973;"City";"Miami";"Weather";15))


  • or query with placeholder. With this way, you can easily create dynamic queries, and 4D converts your variables for you:


advanced example

The previous examples were just to show the syntax, but obviously to find something in a four-element collection, a loop would have been enough. The real strength of the query method is when you need to find something in an object or a collection containing a large quantity of data. The query method is a very efficient way to access data.

The example below shows how easy it is now to find the number of operations an administrator has performed today:

// Save the current log file to JSON and put it in a collection
LOG FILE TO JSON("c:\\ExportLogs")
$txt:=Document to text("c:\\ExportLogs\\JournalExport.json")
$col:=JSON Parse($txt)
// Count how many operations have been done by "Administrator"

Comparison operators

Comparison operators are used to compare one expression to another. The result of a comparison can be TRUE or FALSE. The comparison operators are:

  • =
  • #
  • <
  • >
  • <=
  • >=


logical operators

Logical operators are used to test the validity of a condition. Like comparison operators, they return TRUE or FALSE. The logical operators are:

  • AND
  • OR

$col:=$c.query("Id>50 AND (Weather>10 OR Weather<0)")
$col2:=$c.indices("Id>50 AND (Weather>10 OR Weather<0)")

Fabrice Mainguené
• Product Owner •Fabrice Mainguené joined 4D Program team in November, 2016. As a Product Owner, he is in charge of writing the user stories then translating it to functional specifications. His role is also to make sure that the feature implementation delivered is meeting the customer need.After obtaining a Bachelor degree in Computer Science at CNAM, Fabrice joined a small software publishing company as a Windev developer. Then he worked for different companies in industry and trade areas as a Windev and web developer as well as technical advisor on new features.