Foysal Osmany
Foysal Osmany

Reputation: 1581

How to generate unique UUID when creating bulk node in Neo4j

Neo4j Bulk Insert is getting the same UUID value / getting 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()}' is getting the same value though it should be an unique UUID. How to invoke the function each time a new node is created?

Upvotes: 4

Views: 464

Answers (1)

Michael Hunger
Michael Hunger

Reputation: 41706

Because you only create the query once but then you create one track per array item.

You should add the uuids to your tracklist (as objects)

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

and then use track.track and track.uuid in your query.

Upvotes: 4

Related Questions