How to create a unique UUID when creating a node array in Neo4j

Neo4j Bulk Insert gets the same UUID value / gets the same return value.

/**
 * @method createAlbumAndTracks
 * @param album
 * @param tracks
 * @returns {any}
 */
createAlbumAndTracks(album:any, tracks:any[]):any {
    const query = `
    UNWIND {trackList} as track
    MERGE (a:Albums ${insertQuery(album)})
    CREATE (t:Tracks)
    SET
        t = track,
        t.id = '${uuid()}', // unique UUID expected
        t.created = timestamp()
    MERGE (a)-[r:ALBUM_TRACKS]->(t)
    RETURN t`;

    return db.run(query, Object.assign(album, {trackList: tracks}));
}

t.id = '$ {uuid ()}' gets the same value, although it must have a unique UUID. How to call a function every time you create a new node?

+4
source share
1 answer

Because you only create a request once, and then create one track for an array element.

You must add uuids to your track list (as objects)

 tracklist = [{track:"Track", uuid:uuid()},....]

and then use track.trackand track.uuidin your request.

+4
source

All Articles