如何在Marklogic JSON文档中插入多个三元组?

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

本文关键字:插入 三元组 文档 Marklogic JSON      更新时间:2023-09-26

我试图插入一些三元组,通过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" : {
  . . .
  }
});

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

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

我试着添加这个作为注释,但是它不会用换行来格式化它。所以这只是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" : {
  . . .
  }
});

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);