get()

The .get() method for the Value struct retrieves the value at a certain field for an object, or a certain index for an array. The method takes a &str or a usize as an argument.

Method Syntax

value.get(target)

Example usage

use surrealdb::engine::any::connect;
use surrealdb_types::{ToSql, Value};

#[tokio::main]
async fn main() -> surrealdb::Result<()> {
let db = connect("mem://").await?;
db.use_ns("test").use_db("test").await?;

let mut res = db
.query(
"[
{
a: {
big: [
'nested',
'object'
]
}
},
{
another: {
big: [
'nested',
'object'
]
}
}
];",
)
.await?;
let as_value = res.take::<Value>(0)?;
// Get the value at index 0, field 'a'
// Output: { big: ['nested', 'object'] }
println!("{}", as_value.get(0).get("a").to_sql());
Ok(())
}

As the .get() method will always return a Value, internally a Value::None is returned when nothing is found at a certain index or field. The methods .is_none() and .into_option() can be used on the Value struct to check if the .get() method has returned a non-None value or not.

See also