如何在 UDF 中指定数据库名称?

How to specify database name in UDF?

我编写了小型 UDF(基于文档页面):

function greeting() {
    const db = require('@arangodb').db; 
    let result = db._query('for d in docs FILTER p.id == "123" return d').toArray()[0]
    return result;
}

module.exports = greeting;

它仅适用于默认 _system 数据库的问题。如何指定另一个?

我在文档中找到 mention

Changing the database might be disallowed in some contexts, for example server-side actions (including Foxx).

所以这不可能?!太荒谬了!

来自docs

Internally, UDFs are stored in a system collection named _aqlfunctions of the selected database. When an AQL statement refers to such a UDF, it is loaded from that collection. The UDFs will be exclusively available for queries in that particular database.

如果您有数据库 myDB,请确保为该数据库注册 UDF:

arangosh --server.database myDB ...

或连接到默认数据库并切换到正确的数据库:

db._useDatabase("myDB");
aqlfunctions.register(...);

您不能从一个 UDF 中访问不同的数据库,它被设计为仅限于当前数据库 - 它是 AQL 的扩展机制,在单个数据库的上下文中执行。