Randomly select a document in ArangoDB

此生再无相见时 提交于 2019-12-23 09:59:55

问题


Is there a way to randomly return a document from a collection using AQL?

I would like to create a random graph for testing purposes. I have not yet figured out how to select documents at random from the collection.

I was hoping I might be able to do something like this:

db._query('RETURN nodes[RAND(0..LENGTH(nodes))]').toArray()
JavaScript exception in file '/usr/share/arangodb/js/client/modules/org/arangodb/arangosh.js' at 104,11: [ArangoError 1541: invalid number of arguments for function 'RAND()', expected number of arguments: minimum: 0, maximum: 0 (while parsing)]
!    throw new ArangoError(requestResult);

Any thoughts on how to do this?


回答1:


@yojimbo87 is right.

To select a random document from a collection you can instead do this:

FOR node IN nodes
  SORT RAND()
  LIMIT 1
  RETURN node

Collection objects in the JavaScript layer (arangosh/Foxx) also have a method for that:

var node = db.nodes.any();



回答2:


As far as I know RAND() AQL function doesn't take any parameters and returns pseudo-random number between 0 and 1 which is why you are getting the error about invalid number of arguments.



来源:https://stackoverflow.com/questions/28819675/randomly-select-a-document-in-arangodb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!