Skip to main content

Getting Started with Optic

With a Select Constraint

We want to retrieve the employee IDs and last names of our employees.

An Optic query like this one retrieves certain columns defined in a view:

op.fromView('Employee', 'Profile')
  .select(['GUID', 'Surname'])
  .offsetLimit(0, 100)
  .result();

We used this query to retrieve a row sequence containing the GUID and Surname columns, with a row for each employee in our employee document collection. We limited our output to the first 100 results to make sure our query was working as expected before unleashing it upon the entire collection:

  • The Data Accessor Function fromView() pulls data indexed for the view Profile associated with the schema Employee into a row sequence of this view’s columns.

  • The Operator Function select() constrains the query to only the specified columns.

  • The Operator Function offsetLimit() restricts results returned. The first parameter specifies the number of results to skip; the second, the number of results to return. So, (0, 100) returns the first 100 results.

  • The Executor Function result() executes the query and returns the results as a row sequence.

  • Operator functions like select(), offsetLimit(), and orderBy() must come after any data accessor functions and before any executor function within a query.

  • You can put operator functions in any order; however, each operator function works on the results of the previous operator function, so different orders define different queries.

Here are rows 1-5 of the 100-row x 2-column result:

{
  "Employee.Profile.GUID": "095d4e63-4a1f-4fc1-b694-b681e2aa3ee0",
  "Employee.Profile.Surname": "Morlan"
}
{
  "Employee.Profile.GUID": "f172c249-3f22-4ebb-a29f-aa2b88213d24",
  "Employee.Profile.Surname": "Crider"
}
{
  "Employee.Profile.GUID": "64f1827d-a2bb-40d0-8875-7fc1d03c311b",
  "Employee.Profile.Surname": "Williams"
}
{
  "Employee.Profile.GUID": "c38b7fba-349f-46ff-a210-e329d9f2dbf1",
  "Employee.Profile.Surname": "Inman"
}
{
  "Employee.Profile.GUID": "40c3def5-b544-4611-9a5b-445cb2c4d89b",
  "Employee.Profile.Surname": "Whitfield"
}
  • This query returned the first 100 results as we specified in offsetLimit().

  • The rows are in an unspecified order, which could change between executions. You can specify row order with the orderBy() operator function.

  • The columns are the ones we specified in select().

  • The columns are presented in the order they appear in select().