2016-03-23 27 views
6

Sto provando a creare un sistema di note/commenti per un'area di amministrazione con il nuovo supporto MySQL per JSON. I commenti devono essere modificabili e volevo aggiungere il supporto per altre cose in futuro, magari file allegati (memorizzerei il percorso file in JSON solo non il file stesso!).MySQL 5.7.8 JSON unisce nuovi dati

{ 
    "comments": [ 
    { 
     "comment": "This is a comment", 
     "user_id": 5, 
     "datecreated": "2016-03-19" 
    }, 
    { 
     "comment": "This is a comment", 
     "user_id": 1, 
     "datecreated": "2016-03-19" 
     "comments": [ 
     { 
      "comment": "This is a sub-comment", 
      "user_id": 4, 
      "datecreated": "2016-03-19" 
     }, 
     { 
      "comment": "This is a sub-comment", 
      "user_id": 4, 
      "datecreated": "2016-03-19" 
     } 
     ] 
    } 
    ] 
} 

ho pensato che ci sarebbe un modo per unire in nuovi dati simili a array_merge() senza la necessità di indirizzare una particolare chiave ogni volta.

Questa query funziona ma si rivolge solo a una cosa, il contenuto del testo del commento. Se volessi aggiungere/modificare tag, immagini o file, ecc., Avrei bisogno di una query molto lunga o di più query.

UPDATE shared_notes SET json = JSON_REPLACE(json, "$.comments[1].comment", "This is a test comment") WHERE note_id = :note_id 

Ho provato utilizzando funzioni JSON_REPLACE e JSON_SET con JSON_OBJECT ma sovrascrive tutte le chiavi che non sono specificati, il che significa user_id, DateCreated e osservazioni sub sovrascritti.

UPDATE shared_notes SET json = JSON_REPLACE(json, "$.comments[1]", JSON_OBJECT("comment", "This is a test comment")) WHERE note_id = :note_id 

Questa frankenstein di una query quasi opere ma concatena in realtà il commento aggiornato sulla fine del vecchio:

UPDATE shared_notes SET json = JSON_SET(json, "$.comments[1]", JSON_MERGE(JSON_EXTRACT(json, "$.comments[1]"), CAST('{"comment":"Test"}' AS JSON))) WHERE note_id = :note_id 

Così, c'è un modo migliore per aggiornare facilmente/dinamicamente la JSON utilizzando MySQL o ha come target $.comments[1].comment, $.comments[1][0].user_id ecc. L'unico modo?

+0

Oh uomo, ho appena può sentire il dolore. Non riesco a capire come tali funzioni non facciano parte delle funzioni JSON fornite. È un bisogno così basilare! – EscapeNetscape

risposta

0

Questa è una risposta molto tardi, ma ancora - si può fare in questo modo:

create table sampl_test(id int, comments json); 
insert into sampl_test values(1, 
'{ 
    "comments": [ 
     { 
      "comment": "This is a comment", 
      "user_id": 5, 
      "datecreated": "2016-03-19" 
     }, 
     { 
      "comment": "This is a comment", 
      "user_id": 1, 
      "datecreated": "2016-03-19", 
      "comments": [ 
       { 
        "comment": "This is a sub-comment", 
        "user_id": 4, 
        "datecreated": "2016-03-19" 
       }, 
       { 
        "comment": "This is a sub-comment", 
        "user_id": 4, 
        "datecreated": "2016-03-19" 
       } 
      ] 
     } 
    ] 
} 
') 
; 

select json_merge('{"comments" : {"comment" : "This is a test comment" }}', comments) 

from sampl_test;