dsn46282 2018-07-09 23:27
浏览 112
已采纳

如何在事务中插入文档ID? (arangoDB)

I've been searching the web a lot for clues but can't seem to find any...

public function createNew($name, $type, $restriction,$picture){
   global $connection;
   $trans=new Transaction($connection,array( 'collections' => array( 'write' => array( 'group_relations','groups' ),'read'=> array( 'group_relations','groups' ) ), 'waitForSync' => true ));
   $trans->setAction('function(){
      var db= require("@arangodb").db;
      var arr=db.groups.insert({"name":"'.$name.'","type":"'.$type.'","restriction":"'.$restriction.'","picture":"'.$picture.'"}).toArray();
                db.group_relations.insert({"_from":"users/'.$_SESSION['uid'].'","_to":"groups/"+arr[0]["_id"],"status":"admin"});
      }');
   $trans->execute();
}

this is a PHP function that makes a transaction. In the transaction, I'm trying to create a group, get its id and insert it in the relation collection between the creator and the new group.

Basically make the creator of the group the admin.

"Fatal error: Uncaught triagens\ArangoDb\ServerException: 17 db.groups.insert(...).toArray is not a function".

Any solutions?

  • 写回答

1条回答 默认 最新

  • dpgkg42484 2018-07-10 08:08
    关注

    The result of insert({...}) is always an object. The result of insert([{...},{...},...,{...}]) is always an array. In either case .toArray() does not help.

    So if you'd like to make sure, that you have an array as a result type, please use the array insert also for single entries:

    var arr=db.groups.insert(
      [{"name":"'.$name.'",
        "type":"'.$type.'",
        "restriction":"'.$restriction.'",
        "picture":"'.$picture.'"}]);
    

    So in order to get back the ids from your inserts only:

    var ids = []; 
    db.groups.insert([{...},...,{...}]).forEach(
      function(obj) {ids.push(obj._id)});
    ...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题