在 ArangoDB 中随机选择一个文档 [英] Randomly select a document in ArangoDB

查看:22
本文介绍了在 ArangoDB 中随机选择一个文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法使用 AQL 从集合中随机返回文档?

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?

推荐答案

@yojimbo87 是对的.

@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

JavaScript 层(arangosh/Foxx)中的集合对象也有一个方法:

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

var node = db.nodes.any();

这篇关于在 ArangoDB 中随机选择一个文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆