NEW

The wait is over. SurrealDB 3.0 is here!

Learn more
Course content preview

3: Using the ONLY keyword

SurrealDB returns an array of results by default. But in queries that return a single record, you can add ONLY to return just the record itself instead of an array of records.

-- Return an array with one record inside
CREATE place;
-- Return one record
CREATE ONLY place;

Response

-------- Query 1 --------

[
{
id: place:hke4u4eqkm9lomnma9ac
}
]

-------- Query 2 --------

{
id: place:8xi1lvtx785stp7lyy64
}

This is useful when working with the output of a query. Instead of needing to use [0] to pull out the first item at index 0, the output of the query itself is now just a single record.

It is also helpful when using one of the SDKs and you want a response that can be deserialised easily into a single object.