Select a random document from a collection

Hi,
i have loaded a collection test.anonymization.femaleNames
with 500 documents like those:

{“id”:1, “name”:“Patrícia”}"
{“id”:2, “name”:“Jéssica”}
{“id”:3, “name”:“Laura”}
{“id”:4, “name”:“Maria”}
{“id”:5, “name”:“Marie”}
{“id”:6, “name”:“Nathalie”}
{“id”:7, “name”:“Monique”}
{“id”:8, “name”:“Martine”}
{“id”:9, “name”:“Anne”}
{“id”:10, “name”:“Isabelle”}

{“id”:500, “name”:“Rose”}

fied “id” is also the key.

I want to create a function for recovering a random female name from the collection.

I have created function:
create FUNCTION femaleName(vkey) { (
select name from test.anonymization.femaleNames use keys ‘vkey’) };

my idea is to use the parameter to pass the random key:
execute function femaleName( floor(random()*10));

but function does not work passing the key.
execute function femaleName(1); --returns nothing.

Any help will be appreciated.
thanks.

Your query returns the name of a document where the key is the string ‘vkey’. I suspect you want the document where the key is the value of the argument vkey. There should not be quotes around vkey in the select statement.

See Example 2 here. Notice there are no quotes around vActivity in the select statement.

Ok thanks. removing the quotes from function and using them on the call, fixed the issue:
create FUNCTION femaleName(vkey) { (
select name from test .anonymization.femaleNames use keys vkey) };
EXECUTE FUNCTION femalename(‘1’);
result:
[
[
{
“name”: “Nathalie”
}
]
]

also transforming the random number into string for calling the function always as string:
EXECUTE FUNCTION femaleName( to_string(FLOOR(RANDOM()*10)));

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.