How do I access inserted document's _id within a transaction in ArangoDB -
i don't see examples of accessing inserted/updated documents key's when in transaction.
var collections = {write: ['foo','bar']}; var action = string(function () { var doc = params['doc']; var relateddoc = params['relateddoc']; var db = require('internal').db; db.foo.save(doc); // how access _id, key etc of newly inserted doc? relateddoc.foos.push(doc._id); // _id not exist yet db.bar.save(relateddoc); return {success: true}; }); var params = { doc: doc, relateddoc: relateddoc }; db.transaction(collections, action, params, function (err, result) { if (err) { return dfd.reject(err); } return dfd.resolve(result); });
the collection.save()
method return meta-data saved document:
_rev
: document revision id (auto-generated server)_key
: document key (either specified user in_key
attribute or auto-generated server if not)_id
: same key, including collection name
to use generated id in code, can capture result of collection.save()
in variable , use follows:
var collections = {write: ['foo','bar']}; var action = string(function () { var doc = params['doc']; var relateddoc = params['relateddoc']; var db = require('internal').db; var newdoc = db.foo.save(doc); // capture result of save in newdoc relateddoc.foos.push(newdoc._id); // use newdoc._id db.bar.save(relateddoc); return {success: true}; }); var params = { doc: doc, relateddoc: relateddoc }; db.transaction(collections, action, params, function (err, result) { if (err) { return dfd.reject(err); } return dfd.resolve(result); });
Comments
Post a Comment