如何在 Marklogic JSON 文档中插入一个以上的三元组?

How do I insert more that one triple in a Marklogic JSON document?

我正在尝试通过 Javascript(查询控制台)将一些三元组插入 JSON 形式的文档中

declareUpdate();
xdmp.documentInsert('/aem/5/content/demo-spark/en_GB/automation_article.json',
{
  "triple" : {
    "subject" : "https://content.ea.com/aem/5/content/demo-spark/en_GB/automation_article2.json",
    "predicate" : "https://content.ea.com/iri/author",
    "object" : "jasonmoore"
  },
  "triple" : {
    "subject" : "https://content.ea.com/aem/5/content/demo-spark/en_GB/automation_article2.json",
    "predicate" : "https://content.ea.com/iri/id",
    "object" : "automation_article2"
  },
  "triple" : {
    "subject" : "https://content.ea.com/aem/5/content/demo-spark/en_GB/automation_article2.json",
    "predicate" : "https://content.ea.com/iri/dateCreated",
    "object" : "2015-08-14 09:38:10 GMT-7:00"
  },
  "content" : {
  . . .
  }
});

但是,当我查看新创建的文档时,只有最后一个三元组在那里,其他两个都不见了。

我需要做什么才能在同一文档中获取前两个三元组?

一个JSON对象存储键值对。密钥是唯一的。

var obj = {
  a : 'This is a property, but it will be overwritten',
  a : 'Im really the value of a property'
};

console.log(obj);

也就是这样说:

var obj = {
  a : 'This is a property, but it will be overwritten'
};

obj['a'] = 'Im really the value of a property';

console.log(obj);

现在你可以想想发生了什么:每次你尝试插入键 "triple" 都会覆盖它包含的内容,最终存储的值是最后一个。

var myDbObject = {};
var obj = {
  "triple" : {
    "subject" : "https://content.ea.com/aem/5/content/demo-spark/en_GB/automation_article2.json",
    "predicate" : "https://content.ea.com/iri/author",
    "object" : "jasonmoore"
  },
  "triple" : {
    "subject" : "https://content.ea.com/aem/5/content/demo-spark/en_GB/automation_article2.json",
    "predicate" : "https://content.ea.com/iri/id",
    "object" : "automation_article2"
  },
  "triple" : {
    "subject" : "https://content.ea.com/aem/5/content/demo-spark/en_GB/automation_article2.json",
    "predicate" : "https://content.ea.com/iri/dateCreated",
    "object" : "2015-08-14 09:38:10 GMT-7:00"
  }
};

Object.keys(obj).forEach(key=>{
  myDbObject[key] = obj[key];
});

console.log(myDbObject);

我试图将其添加为评论,但它不会使用换行符对其进行格式化。所以这只是 Jose Hermosilla Rodrigo 的答案的延伸。

因为不能有很多同名的对象键,所以使用数组:

declareUpdate();
xdmp.documentInsert('/aem/5/content/demo-spark/en_GB/automation_article.json',
{ "triples": [
  { "triple": {
    "subject" : "https://content.ea.com/aem/5/content/demo-spark/en_GB/automation_article2.json",
    "predicate" : "https://content.ea.com/iri/author",
    "object" : "jasonmoore"
  }},
  { "triple": {
    "subject" : "https://content.ea.com/aem/5/content/demo-spark/en_GB/automation_article2.json",
    "predicate" : "https://content.ea.com/iri/id",
    "object" : "automation_article2"
  }},
...
],
  "content" : {
  . . .
  }
});