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

9rnv2umw  于 2022-12-09  发布在  Go
关注(0)|答案(2)|浏览(134)

如果在一个特定的ArangoDB服务器上定义了多个数据库,如何指定要运行AQL查询的数据库?
通过包含数据库名称(替换为下面的[DBNAME])的REST端点运行查询,即:

/_db/[DBNAME]/_api/cursor

似乎不起作用。错误消息显示“未知路径/_db/[数据库名]/_API/cursor”
这是我必须在查询本身中指定的内容吗?
另外:我尝试运行的查询是:

FOR col in COLLECTIONS() RETURN col.name

顺便说一句,我还没有找到一种通过REST API设置“当前”数据库的方法。而且,我正在使用fuerte从C++访问REST API。

7cwmlq89

7cwmlq891#

***Tom Regner***在此值得称赞,因为他提出了一项调查,得出了这个答案。我在此发布我的发现,作为一个答案,以帮助其他可能遇到这个问题的人。

我不知道这是一个缺陷,缺点或只是一个API的警告,我不清楚...但...
要在a::arangodb::fuerte::Request的标头中注册和使用终结点中的'/_db/[DBNAME/'前缀(例如完整终结点'/_db/[DBNAME/_API/cursor'),仅调用以下命令是不够的(从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);

如果不这样做,可能会导致关于**“未知路径”**的错误。

**注1:**只需设置数据库成员变量,即

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

不起作用。

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

zaqlnxep

zaqlnxep2#

这些文档现在并不是很有用,所以如果有人想找一个更完整的例子,请考虑下面的代码。

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;

相关问题