- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我在 ArangoDB+nodejs 中的事务有一些问题。我需要做这样的事情:
transaction
{
insertedId=insertItemOneInDB();
insertItemTwoInDB();
}
但是当第二次插入失败时,第一次没有回滚!请帮我举个例子!
这是我的代码:
var transaction = function (collections,params,callback)
{
try
{
db.transaction.submit("user_merchant_tbl",params,
function ()
{
console.log("_collections:",collections);
console.log("params:");
console.log(params);
console.log("---");
console.log("+==========+");
//
var insertedDataId;
var relationsArrayIds=[];
db.document.create(collections,params.data).then(function(_insertedId)
{
insertedDataId=_insertedId;
}
,function(err)
{
console.log("ERROR: Arango--insert-->err: %j", err);
//throw "Error: "+err;
return false;
});
/////
var relations=params.relations;
for(var i=0;i<relations.length;i++)
{
db.document.create(relations[i].edge,relations[i].data).then(
function(_id)
{
relationsArrayIds.push(_id);
next(true);
}
,function(err)
{
console.log("ERROR: Arango--insert.edge-->err:23232 %j", err);
console.log("after return");
next(false);
return false
});
}
console.log("transaction before true");
function next(result)
{
if(result==true)
{
console.log("transaction is ok:",result);
callback(insertedDataId,result);
}
else
{
console.log("transaction is not OK:",result);
callback(insertedDataId,false);
}
}
}
);
}
catch(e)
{
console.log("catch->error in -->Arango.transaction: ",e);
}
}
最佳答案
首先,似乎对如何编写应该执行的操作存在误解。此操作直接在数据库服务器上执行,因此您不能使用 Arango Javascript api 提供的任何功能。如果你想设计你的 Action ,它必须在 arango shell 或服务器控制台上运行(bin/arangod data --console)我查看了您的代码并假设您想要存储用户和商家之间的关系。由于 Arango 带有一个很好的图形模块,您可以遵循以下方法:
// First we define a graph, containing of 2 document collections ("users" and "merchants") and 2 edge collections (one per relation type, in this example "contactRequested" and "boughtSomethingFrom".
// Note that in this definition the relation "boughtSomethingFrom" is only allowed from a user to a merchant. Of course this is just one way to design it, you have to do it the way it suits you the best.
var edgeDefinitions = [{
collection: "contactRequested",
from: ["users", "merchants"],
to: ["users", "merchants"]
}, {
collection: "boughtSomethingFrom",
from: ["users"],
to: ["merchants"]
}];
// Now we create a graph called "user_merchant_graph" and in the callback function execute a transaction
db.graph.create("user_merchant_graph", edgeDefinitions, function(err, ret, message) {
// Lets define the action for the transaction, again this will be executed directly on the server ......
var action = function (params) {
// We have to require the database module ....
var db = require("internal").db;
var relationsArrayIds = [];
// now we store the user provided to the function
var insertedUserId = db["users"].insert(params.data)._id;
var relations = params.relations;
// Now we loop over through the relations object, store each merchant and it's relations to the user
Object.keys(relations).forEach(function (relation) {
// store merchant
var insertedMerchantId = db["merchants"].insert({merchantName : relation})._id;
// store relation as edge from "insertedUserId" to "insertedMerchantId".
var edgeId = db[relations[relation].relation].insert(insertedUserId, insertedMerchantId, relations[relation].additionalData)._id;
relationsArrayIds.push(edgeId);
});
};
// End of action
var options = {};
options.params = {
data: {
userName : "someUserName",
userSurname : "someUserSurname"
},
relations : {
merchantA : {relation : "contactRequested", additionalData : {data :"someData"}},
merchantB : {relation : "boughtSomethingFrom", additionalData : {data :"someData"}},
merchantC : {relation : "contactRequested", additionalData : {data :"someData"}}
}
};
// Now we call the transaction module ... a note to the collections parameter, it has to be an object containing the keys "write" and "read" which have a list of all collections as value into which the action is writing /reading from
// This collections object is NOT available within your action, the only thing passed as argument to your action is "options.params" !!
db.transaction.submit({write : ["users", "merchants", "contactRequested", "boughtSomethingFrom"]}, action, options, function(err, ret, message) {
//some callback
});
});
关于他们正在处理的交易,您可以试一试这段代码,如果您觉得。弄乱边的存储(将其更改为“var edgeId = db[relations[relation].relation].insert(relations[relation].additionalData)._id;”)你会看到你的用户和商户还没有被存储
希望对你有帮助
关于node.js - Arangodb 中的交易,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26466800/
我无法停止 arangodb docker 容器中的 arangodb 服务器。 当我执行低于一个时, $/etc/init.d/arangod 停止 $service arangodb 状态 ara
我在 java 中查询 ArangoDB 以获取数组值时遇到问题。我尝试过使用 String[] 和 ArrayList,都没有成功。 我的查询: FOR document IN documents
我想计算 ArangoDB 的服务器要求。 我知道 ArangoDB 将索引存储在 RAM 中,但是索引使用了多少空间? 最佳答案 这取决于您使用的索引类型。 您可以使用“数字”来查看需要多少内存:
我是 Arango DB 的新用户,目前正在为我的项目评估它。有人可以告诉我,您可以在 Arango DB 中创建的最大数据库数量是多少吗? 谢谢。 最佳答案 据我所知,ArangoDB 中的数据库数
我尝试从我的电脑连接到位于另一台服务器上的 ArangoDB,但似乎不成功。然后,我尝试使用输入服务器 ip http://x.x.x.x:8529 提供的 Web UI 来访问它。但也失败了。我在本
我有一个这样的文件: { "baths": 2, "beds": 3, "id": "3225C", "addrs": [ { "line2": "",
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
检索相关edge_collection中没有边的所有顶点的最佳方法是什么 我尝试使用以下代码,但自 arangodb 2.8 以来,它变得非常慢(在以前的版本中并不是很快,但比现在快了大约 10 倍)
我有许多节点通过其他类型的中间节点连接。如图所示,中间节点可以有多个。我需要找到给定数量的节点的所有中间节点,并按初始节点之间的链接数量对其进行排序。在我的示例中,给定 A、B、C、D,它应该返回节点
我有一个存储在 arangodb 中的对象,该对象具有其他内部对象,我当前的用例要求我仅更新其中一个元素。 存储对象 { "status": "Active", "physicalCode":
我正在尝试与 Arango 组合一个单元测试设置。为此,我需要能够在每次测试时重置测试数据库。 我知道我们可以直接从 REST API 删除数据库,但文档中提到创建和删除可能“需要一段时间”。 这是否
我有某种类型的事件列表,其结构如下: { createdAt: 123123132, type: STARTED, metadata: { emailAddress: "foo@bar
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 3 年前。
我发现使用 Web UI 在 arangodb 上创建新用户时遇到了麻烦。首先,我可以创建一个新用户,但无法为该用户分配一个数据库。另外,你能帮忙提供一个使用 arangodb Shell 的方法吗?
我在本地运行 ArangoDB,其中包含来自多个不同项目的数据库、集合、数据和图表。我想备份所有内容,以便重建我的系统。我知道如何备份单个数据库,但因为我有很多数据库,所以我希望一次性完成。 本质上,
应该很容易操作,但我找不到如何实现这一点。我有两个来自不同集合的文档,现在我想使用现有集合中的新 Edge 链接它们。我正在尝试像这样使用 edge-collection.save 函数:edge-c
在我解决 1000 个客户端的任务中,每个客户端都有单独的 ArangoDB,在运行时记录单独的信息......我们需要将这些信息聚合回单个主节点服务器中的集合,以便可以进行查询和报告在上面。研究 J
具有以下结构的文档: { path: String, enabled: Long, disabled: null || Long, // other fields... } 我想通过路
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 3 年前。
我想从 ArangoDB 的文档中删除一个属性。 我认为正确的方法是使用函数 UNSET(doc, attributeName1, ..., attributeNameN) .然而,仅凭这一点,数据库
我是一名优秀的程序员,十分优秀!