简体   繁体   English

在ArangoDB中随机选择一个文档

[英]Randomly select a document in ArangoDB

Is there a way to randomly return a document from a collection using AQL? 有没有办法使用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? 有关如何做到这一点的任何想法?

@yojimbo87 is right. @ yojimbo87是对的。

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: JavaScript层(arangosh / Foxx)中的集合对象也有一个方法:

var node = db.nodes.any();

据我所知, RAND() AQL函数不接受任何参数并返回0到1之间的伪随机数,这就是为什么你得到关于无效参数数量的错误的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM