如何在 ArangoDb AQL 查询中指定数据库?

How to specify the database in an ArangoDb AQL query?

如果在特定的 ArangoDB 服务器上定义了多个数据库,我该如何指定我希望 AQL 查询针对 运行 的数据库?

运行 通过包含数据库名称的 REST 端点的查询(替换为下面的 [DBNAME])即:

/_db/[DBNAME]/_api/cursor

好像不行。错误消息显示 'unknown path /_db/[DBNAME]/_api/cursor'

这是我必须在查询本身中指定的内容吗?

另外:我正在尝试 运行 的查询是:

FOR col in COLLECTIONS() RETURN col.name

首先,我还没有找到通过 REST API 设置 "current" 数据库的方法。另外,我正在使用 fuerte 从 C++ 访问 REST API。

Tom Regner 在这里值得主要赞扬,因为他提出了产生此答案的询问。我在这里发布我的发现作为答案,以帮助可能 运行 参与其中的其他人。

我不知道这是一个严重的错误、缺点还是只是一个我不清楚的 api 警告...但是...

为了端点中的'/_db/[DBNAME/'前缀(例如完整端点'/_db/[DBNAME/_api/cursor ') 在 ::arangodb::fuerte::Request 的 header 中注册和使用,这是不够的(从 arangodb 3.5.3 和当时可用的 fuerte 版本开始)这个答案)简单地调用:

std::unique_ptr<fuerte::Request> request;
const char *endpoint = "/_db/[DBNAME/_api/cursor";
request = fuerte::createRequest(fuerte::RestVerb::Post,endpoint);
// and adding any arguments to the request using a VPackBuilder...
// in this case the query (omitted)

要将数据库名称包含在此类请求中,您必须另外调用以下命令:

request->header.parseArangoPath(endpoint);

不这样做似乎会导致有关 'unknown path' 的错误。

注1:简单设置数据库成员变量,即

request->header.database = "[DBNAME]";

无效。

注意 2: 没有前导 '/_db/[DBNAME]/' 前缀的操作似乎使用'current' 数据库。 (至少对我来说,这似乎停留在“_system”,因为据我所知,似乎没有端点可以通过 HTTP REST Api 来更改它。)

文档目前不是很有用,所以如果有人正在寻找更完整的示例,请考虑以下代码。

EventLoopService eventLoopService;
// adjust the connection for your environment!
std::shared_ptr<Connection> conn = ConnectionBuilder().endpoint("http://localhost:8529")
        .authenticationType(AuthenticationType::Basic)
        .user(?)      // enter a user with access
        .password(?)  // enter the password
        .connect(eventLoopService);

// create the request
std::unique_ptr<Request> request = createRequest(RestVerb::Post, ContentType::VPack);

// enter the database name (ensure the user has access)
request->header.database = ?;

// API endpoint to submit AQL queries
request->header.path = "/_api/cursor";

// Create a payload to be submitted to the API endpoint
VPackBuilder builder;
builder.openObject();
// here is your query
builder.add("query", VPackValue("for col in collections() return col.name"));
builder.close();

// add the payload to the request
request->addVPack(builder.slice());

// send the request (blocking)
std::unique_ptr<Response> response = conn->sendRequest(std::move(request));

// check the response code - it should be 201
unsigned int statusCode = response->statusCode();

// slice has the response data
VPackSlice slice = response->slices().front();

std::cout << slice.get("result").toJson() << std::endl;