简体   繁体   中英

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

I am trying to insert some triples, via Javascript (query console), into a JSON document of the form

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" : {
  . . .
  }
});

However, when I look in the newly created document, only the last triple is there, the other two are missing.

What do I need to do to get the first two triples in the same document?

I tried to add this as a comment, but it won't format it with line breaks. So this is just an extension of the answer from Jose Hermosilla Rodrigo.

Since you can't have many object keys with the same name, use an array:

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" : {
  . . .
  }
});

A JSON object stores key-value pairs. The keys are unique.

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

That's the same to say :

 var obj = { a : 'This is a property, but it will be overwritten' }; obj['a'] = 'Im really the value of a property'; console.log(obj); 

Now you can think what's happening: Everytime you try to insert in the key "triple" is overwritting what it contains, and the value that finally stores is the last one.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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