简体   繁体   English

在ArangoDB中插入数组值

[英]Insert on array value in ArangoDB

I have a document just like this. 我有一个像这样的文件。

{ "Node": { "-name": "Dev6", "Interface": [ { "-ip": "10.20.18.65", "-mask": "255.255.255.192" }, { "-ip": "10.20.18.129", "-mask": "255.255.255.192" } ] } } {“节点”:{“-名称”:“ Dev6”,“接口”:[{“ -ip”:“ 10.20.18.65”,“ -mask”:“ 255.255.255.192”},{“ -ip”: “ 10.20.18.129”,“ -mask”:“ 255.255.255.192”}]}}

My perl program is following. 我的perl程序正在关注。

my $dbs_update_Node_by_key ='FOR u IN Node FILTER u._key == @key  UPDATE u WITH {
    name: @name,
    Interface: @Interface
} IN Node';

...... 
(comments: $inf means [{"-ip","-mask"},{"-ip","-mask"}])

my $bind_args = {
key => $doc->{'_key'},
name => $node_attrs->{'-name'},
Interface => $inf
};

$sth = $itdb->query($dbs_update_Node_by_key)->bind($bind_args)->execute();

It returns "Invalid bind parameter value". 它返回“无效的绑定参数值”。 I think ArangoDB perl driver didn't support it. 我认为ArangoDB perl驱动程序不支持它。 How can I use AQL or REST API to implement it? 如何使用AQL或REST API实施它? Thanks! 谢谢!

I think the problem is that 我认为问题是

[{"-ip","-mask"},{"-ip","-mask"}]

won't work. 将无法正常工作。 When using the curly brackets and member names (eg "-ip", "-mask"), there must be a value associated to each member. 使用大括号和成员名称(例如“ -ip”,“-mask”)时,必须为每个成员都关联一个值。 Using this value instead should work: 使用此值应该可以:

[{"-ip": "a.b.c.d", "-mask": "a.b.c.d" }, {"-ip": "a.b.c.d" ,"-mask": "a.b.c.d" }]

Please also note that in your above query, you will update an attribute named "name", whereas in the example document the attribute name is "-name" (with minus sign in front). 还请注意,在上面的查询中,您将更新名为“ name”的属性,而在示例文档中,属性名称为“ -name”(前面带有减号)。 To use an attribute name with a minus sign at the beginning, it needs to be quoted in backticks in AQL (see below). 要在开头使用带减号的属性名称,需要在AQL的反引号中将其引起来(请参见下文)。

Additionally, the example document has attributes "-name" and "Interface" inside a sub-attribute "Node", whereas the UPDATE command will update attributes "name" and "Interface" on the top level of the document. 此外,示例文档在子属性“节点”内具有属性“ -name”和“接口”,而UPDATE命令将在文档的顶层更新属性“名称”和“接口”。

I have adjusted the query a bit. 我对查询做了一些调整。 The following sequence seems to work from the ArangoShell: 以下序列似乎在ArangoShell中有效:

db._create("Node"); 
db.Node.save({ 
  "_key": "test", 
  "Node": { 
    "someAttribute": "someValue", 
    "-name": "Dev6",  
    "Interface": [ 
      { 
        "-ip": "10.20.18.65", 
        "-mask": "255.255.255.192" 
      }, 
      { 
        "-ip": "10.20.18.129", 
        "-mask": "255.255.255.192" 
      } 
    ] 
  } 
}); 

dbs_update_Node_by_key = 'FOR u IN Node FILTER u._key == @key ' +
  'UPDATE u WITH { Node: { `-name`: @name, Interface: @Interface } } IN Node'; 

bind_args = { 
  key: "test", 
  name: "Dev8", 
  Interface: [
    {
      "-ip": "8.8.8.8", 
      "-mask": "255.255.255.192" 
    }, 
    {
      "-ip": "192.168.0.1",
      "-mask": "255.255.255.255" 
    }
  ] 
}; 

db._query(dbs_update_Node_by_key, bind_args); 
db.Node.toArray();

This will produce: 这将产生:

[ 
  { 
    "_id" : "Node/test", 
    "_key" : "test", 
    "_rev" : "18996044030550", 
    "Node" : { 
      "-name" : "Dev8", 
      "someAttribute" : "someValue", 
      "Interface" : [ 
        { 
          "-ip" : "8.8.8.8", 
          "-mask" : "255.255.255.192" 
        }, 
        { 
          "-ip" : "192.168.0.1", 
          "-mask" : "255.255.255.255" 
        } 
      ] 
    } 
  } 
]

I am not sure if this is what you required, but at least it updates the document and overwrites the "Interface" attribute with new values. 我不确定这是否是您所需要的,但是至少它会更新文档并用新值覆盖“接口”属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM