如何使用 ArangoJs 在 ArangoDb 图中存储文档? [英] How to store documents in an ArangoDb graph using ArangoJs?

查看:22
本文介绍了如何使用 ArangoJs 在 ArangoDb 图中存储文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 nodejs 应用程序中最新版本的 ArangoDb 和 ArangoJs.我有以下两个顶点

I am using latest version of ArangoDb and ArangoJs from a nodejs application. I have got following two vertexes

  1. 用户
  2. 代币

tokens 顶点包含发给 users 顶点中的用户之一的安全令牌.我有一个名为 token_belongs_to 的边定义,将 tokens 连接到 users

tokens vertex contain the security tokens issues to one of the user in the users vertex. I have got an edge definition named token_belongs_to connecting tokens to users

如何使用 ArangoJs 存储属于现有用户的新生成的令牌?

How do I store a newly generated token belonging to an existing user using ArangoJs?

推荐答案

我将假设您正在使用 ArangoDB 2.7 和最新版本的 arangojs(在撰写本文时为 4.1),因为 API 已经发生了一些变化驱动程序的 3.x 版本.

I am going to assume you are using ArangoDB 2.7 with the latest version of arangojs (4.1 at the time of this writing) as the API has changed a bit since the 3.x release of the driver.

你没有提到使用 Graph API 最简单的方法是直接使用集合.但是,使用 Graph API 会增加一些好处,例如在删除任何顶点时会自动删除孤立边.

As you don't mention using the Graph API the easiest way is to simply use the collections directly. Using the Graph API however adds benefits like orphaned edges automatically being deleted when any of their vertices are deleted.

首先,您需要获得对要使用的每个集合的引用:

First you need to get a reference to each collection you want to work with:

var users = db.collection('users');
var tokens = db.collection('tokens');
var edges = db.edgeCollection('token_belongs_to');

或者,如果您使用的是图形 API:

Or if you are using the Graph API:

var graph = db.graph('my_graph');
var users = graph.vertexCollection('users');
var tokens = graph.vertexCollection('tokens');
var edges = graph.edgeCollection('token_belongs_to');

为了为现有用户创建令牌,您需要知道用户的 _id.文档的_id由集合名称(users)和文档的_key(例如12345678)组成代码>).

In order to create a token for an existing user, you need to know the _id of the user. The _id of a document is made up of the collection name (users) and the _key of the document (e.g. 12345678).

如果您没有 _id_key,您还可以通过其他一些唯一属性查找文档.例如,如果您有一个知道其值的唯一属性 email,您可以这样做:

If you don't have the _id or _key you can also look up the document by some other unique attribute. For example, if you have a unique attribute email that you know the value of, you could do this:

users.firstExample({email: 'admin@example.com'})
.then(function (doc) {
  var userId = doc._id;
  // more code goes here
});

接下来您要创建令牌:

tokens.save(tokenData)
.then(function (meta) {
  var tokenId = meta._id;
  // more code goes here
});

一旦你有了 userId 和 tokenId,你就可以创建边来定义两者之间的关系:

Once you have the userId and tokenId you can create the edge to define the relation between the two:

edges.save(edgeData, userId, tokenId)
.then(function (meta) {
  var edgeId = meta._id;
  // more code goes here
});

如果您不想在边缘上存储任何数据,您可以用一个空对象替换 edgeData 或简单地将其写为:

If you don't want to store any data on the edge you can substitute an empty object for edgeData or simply write it as:

edges.save({_from: userId, _to: tokenId})
.then(...);

所以完整的例子应该是这样的:

So the full example would go something like this:

var graph = db.graph('my_graph');
var users = graph.vertexCollection('users');
var tokens = graph.vertexCollection('tokens');
var edges = graph.edgeCollection('token_belongs_to');

Promise.all([
  users.firstExample({email: 'admin@example.com'}),
  tokens.save(tokenData)
])
.then(function (args) {
  var userId = args[0]._id; // result from first promise
  var tokenId = args[1]._id; // result from second promise
  return edges.save({_from: userId, _to: tokenId});
})
.then(function (meta) {
  var edgeId = meta._id;
  // Edge has been created
})
.catch(function (err) {
  console.error('Something went wrong:', err.stack);
});

这篇关于如何使用 ArangoJs 在 ArangoDb 图中存储文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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