Skip to content

Projection

Projection is choosing which data is returned from each matching record, analogous to choosing columns in a relational database using SELECT. Aerospike distinguishes two types of projection:

  • Bin projection — Return values from one or more named bins (a vertical subset of the record’s bin data).
  • Operation projection — Return the results of read-only bin operations, including simple bin reads, path expressions, and other read operations described under operation expressions. This supports nested paths, computed values, and lightweight server-side shaping without an extra round trip.

Uniform API across commands

Single-record operate and batched operate have always supported both bin projection and operation projection in one command: you specify an ordered list of read operations, which can be plain bin reads or richer read operations.

Prior to Aerospike Database 8.1.2, foreground queries supported only a list of bin names (bin projection). Starting with 8.1.2, queries also support operation projection, so the query result shape matches what you can request from operate on a single key or in a batch.

Specifying a list of bin names for projection remains supported. Performance of that form is equivalent to using corresponding bin read operations in the projection list.

Benefits

  • Unified API: Queries behave like batched operations for projections, with a predictable interface across single-key, batch, and query commands.
  • Path expressions in query projection: Use path expressions and other expression APIs in the projection, not just in the filter selection.
  • Server-side computation: Offload lightweight transformations, arithmetic, and aggregations to the server; only requested bins or results are sent back.
  • Lower latency: Avoid the two-phase query-then-batch pattern; one query returns the projected data.

Supported projection operations

Query projection accepts read-only operations:

  • Bin read operations: Operation.get(binName) or equivalent in your client. Equivalent to specifying bin names.
  • CDT read operations: Map and List read operations (e.g., MapOperation.getByKey, ListOperation.getByIndex).
  • Read expressions: Operation expressions that compute a value and return it in a computed bin via ExpOperation.read().

Example: Replacing the two-phase pattern

Before (two-phase): Query with no bins to get keys, then operate batched commands to get projected data.

// Phase 1: Query returns only keys (no bin data)
queryPolicy.includeBinData = false;
RecordSet recordSet = client.query(queryPolicy, stmt);
List<Key> keysList = new ArrayList<>();
while (recordSet.next()) {
keysList.add(recordSet.getKey());
}
recordSet.close();
// Phase 2: Batch operate to get city and state from report map
Key[] keys = keysList.toArray(new Key[0]);
BatchResults batchResult = client.operate(null, null, keys,
MapOperation.getByKeyList("report",
Arrays.asList(Value.get("city"), Value.get("state")),
MapReturnType.VALUE)
);

After (single query with projection ops): One query returns the projected data.

stmt.setOperations(
MapOperation.getByKeyList("report",
Arrays.asList(Value.get("city"), Value.get("state")),
MapReturnType.VALUE)
);
RecordSet recordSet = client.query(null, stmt);
while (recordSet.next()) {
Record record = recordSet.getRecord();
}
recordSet.close();

Example: Read expression in projection

Use a read expression to compute a value and return it in a computed bin:

Expression readExp = Exp.build(
Exp.sub(Exp.intBin("posted"), Exp.intBin("occurred"))
);
stmt.setOperations(
ExpOperation.read("daysToPost", readExp, ExpReadFlags.DEFAULT)
);
RecordSet recordSet = client.query(null, stmt);
Feedback

Was this page helpful?

What type of feedback are you giving?

What would you like us to know?

+Capture screenshot

Can we reach out to you?