4D Blog

Home Product Query collections

Query collections

March 1, 2018

Product

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))

$col:=$c.query("Weather#0")
$col2:=$c.indices("Weather#0")

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

$w:=$1
$col:=$c.query("Weather#:1";$w)
$col2:=$c.indices("Weather#:1";$w)

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"
$count:=$col.query("extraData.user_name=:1";"Administrator").length

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:

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

$col:=$c.query("Weather>0")
//$col=[{Id:128,City:Paris,Weather:1},{Id:2973,City:Miami,Weather:15}]
$col2:=$c.indices("Weather>0")
//$col2=[0,3]

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)")
//$col=[{Id:1792,City:Tokyo,Weather:-5},{Id:2973,City:Miami,Weather:15}]
$col2:=$c.indices("Id>50 AND (Weather>10 OR Weather<0)")
//$col2=[2,3]

Discuss

Tags Collections, Find, v16 R6, v17

Latest related posts

  • May 20, 2025

    Automatic Refresh of OEM Build Licenses

  • May 16, 2025

    New class to handle incoming TCP connections

  • April 29, 2025

    Discover your AI-powered writing assistant in 4D Write Pro

Fabrice Mainguené
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.
  • Deutsch
  • Français
  • English
  • Português
  • Čeština
  • Español
  • Italiano
  • 日本語

Categories

Browse categories

  • 4D View Pro
  • 4D Write Pro
  • 4D for Mobile
  • Email
  • Development Mode
  • 4D Language
  • ORDA
  • User Interface / GUI
  • Qodly Studio
  • Server
  • Maintenance
  • Deployment
  • 4D Tutorials
  • Generic
  • 4D Summit sessions and other online videos

Tags

4D-Analyzer 4D for Android 4D for iOS 4D NetKit 4D Qodly Pro 4D View Pro 4D Write Pro 20 R8 20 R9 Administration Authentication Build application CI/CD Class Client/Server Code editor Collections Compatibility settings Formula Google Listbox Logs Mail Network Objects ORDA PDF Pictures Preemptive Programming Qodly Studio REST Scalability Security Session Source control Speed Spreadsheet Tutorial UI User Experience v20 vscode Web Word processor

Tags

4D-Analyzer 4D for Android 4D for iOS 4D NetKit 4D Qodly Pro 4D View Pro 4D Write Pro 20 R8 20 R9 Administration Authentication Build application CI/CD Class Client/Server Code editor Collections Compatibility settings Formula Google Listbox Logs Mail Network Objects ORDA PDF Pictures Preemptive Programming Qodly Studio REST Scalability Security Session Source control Speed Spreadsheet Tutorial UI User Experience v20 vscode Web Word processor
Subscribe to 4D Newsletter

© 2025 4D SAS - All rights reserved
Terms & Conditions | Legal Notices | Data Policy | Cookie Policy | Contact us | Write for us


Subscribe to 4D Newsletter

* Your privacy is very important to us. Please click here to view our Policy

Contact us

Got a question, suggestion or just want to get in touch with the 4D bloggers? Drop us a line!

* Your privacy is very important to us. Please click here to view our Policy