Imagine the following situation:
INSERT INTO dog (id, age) VALUES ("mr_whaf", 8), ("mrs_whaf", 3), ("whaf_jr", 0), ("whaf_whaf", 1);
RELATE [dog:mr_whaf, dog:mrs_whaf]->parent_of->dog:whaf_jr;
RELATE dog:mrs_whaf->parent_of->dog:whaf_whaf;
I would like to select only dogs having a parent aged more than 5
.
Here is where I am stuck:
SELECT id,age, <-parent_of<-(dog WHERE age > 5).* AS parents FROM dog WHERE age < 2;
-- Query 1 (execution time: 271.399µs)
[
{
age: 0,
id: dog:whaf_jr,
parents: [
{
age: 8,
id: dog:mr_whaf
}
]
},
{
age: 1,
id: dog:whaf_whaf,
parents: []
}
]
I would like only whaf_jr
to appear in the answer.
SELECT id,age, <-parent_of<-(dog WHERE age > 5).* AS parents FROM dog WHERE age < 2 AND count(parents) > 0;
-- Query 1 (execution time: 156.688µs)
[]
SELECT id,age, <-parent_of<-(dog WHERE age > 5).* AS parents FROM dog WHERE age < 2 AND parents.len() > 0;
-- Query 1 (execution time: 167.568µs)
'There was a problem running the len() function. no such method found for the none type'
Any ideas will be appreciated.
Nice example! One quick way is to wrap this in return and filtering on the array: https://surrealdb.com/docs/surrealql/datamodel/arrays#mapping-and-filtering-on-arrays
(SELECT id,age, <-parent_of<-(dog WHERE age > 5).* AS parents FROM dog WHERE age < 2)[WHERE parents];
WHERE parents
goes into the parents
field to see if it is truthy or not (truthy = not NONE, NULL, or an empty value).