using mergeV/E

Hello I’m trying to upgrade my gremlin package to latest and running into an issue with mergeE and mergeV. I’m using this to replace some coalace steps that I was using before. How do you set the label in mergeV() for the particular vertex. I see I can pass the T.label in the map but, I don’t understand what the equivalent of this would be for JavaScript
Solution:
Another approach would be to inject a list of maps and do something like this: ``` var newNodes = [] newNodes.push(new Map([[(t.id),1],...
Jump to solution
4 Replies
triggan
triggan2y ago
This is how I got it to work...
const gremlin = require('gremlin');
const traversal = gremlin.process.AnonymousTraversalSource.traversal;
const __ = gremlin.process.statics;
const DriverRemoteConnection = gremlin.driver.DriverRemoteConnection;
const Graph = gremlin.structure.Graph;
const t = gremlin.process.t;
const merge = gremlin.process.merge;

dc = new DriverRemoteConnection('ws://localhost:8182/gremlin',{});

const graph = new Graph();
const g = graph.traversal().withRemote(dc);

g.mergeV(new Map([[(t.id),1]])).
option(merge.onCreate,
new Map([
[(t.id),1],
[(t.label),'mylabel'],
['name','newname'],
['address','newaddress']])).
option(merge.onMatch,
new Map([
['name','newname'],
['address','newaddress']])).
next().
then(data => {
console.log(data);
dc.close();
}).catch(error => {
console.log('ERROR', error);
dc.close();
});
const gremlin = require('gremlin');
const traversal = gremlin.process.AnonymousTraversalSource.traversal;
const __ = gremlin.process.statics;
const DriverRemoteConnection = gremlin.driver.DriverRemoteConnection;
const Graph = gremlin.structure.Graph;
const t = gremlin.process.t;
const merge = gremlin.process.merge;

dc = new DriverRemoteConnection('ws://localhost:8182/gremlin',{});

const graph = new Graph();
const g = graph.traversal().withRemote(dc);

g.mergeV(new Map([[(t.id),1]])).
option(merge.onCreate,
new Map([
[(t.id),1],
[(t.label),'mylabel'],
['name','newname'],
['address','newaddress']])).
option(merge.onMatch,
new Map([
['name','newname'],
['address','newaddress']])).
next().
then(data => {
console.log(data);
dc.close();
}).catch(error => {
console.log('ERROR', error);
dc.close();
});
edolivares
edolivaresOP2y ago
Thank you! Is that really the only way? Creating the verticies and adding properties was just as easy as using mergeV and passing a map as the argument the only thing is that these only have the default node label) I understand the need to pass the traverser for the T.label value but, why is the id passed twice in this case and the need for the option? It kind of defeats the purpose of replacing the coalace pattern if it’s just as complicated especially since setting the label is essential in a graph
Solution
triggan
triggan2y ago
Another approach would be to inject a list of maps and do something like this:
var newNodes = []
newNodes.push(new Map([[(t.id),1],
[(t.label),'Person'],
['name','Tom'],
['address','101 Elm Street']]));
newNodes.push(new Map([[(t.id),2],
[(t.label),'Person'],
['name','Sally'],
['address','55 Main Street']]));
newNodes.push(new Map([[(t.id),3],
[(t.label),'Person'],
['name','Jane'],
['address','222 5th Avenue']]));

g.inject(newNodes).unfold().mergeV(limit(local,1)). //match on ID only
next().
then(data => {
console.log(data);
}).catch(error => {
console.log('ERROR', error);
});
var newNodes = []
newNodes.push(new Map([[(t.id),1],
[(t.label),'Person'],
['name','Tom'],
['address','101 Elm Street']]));
newNodes.push(new Map([[(t.id),2],
[(t.label),'Person'],
['name','Sally'],
['address','55 Main Street']]));
newNodes.push(new Map([[(t.id),3],
[(t.label),'Person'],
['name','Jane'],
['address','222 5th Avenue']]));

g.inject(newNodes).unfold().mergeV(limit(local,1)). //match on ID only
next().
then(data => {
console.log(data);
}).catch(error => {
console.log('ERROR', error);
});
triggan
triggan2y ago
Something that was not clear to me was how we handled situations where there's not an exact match for a vertex/edge and all associated properties. So if you are just adding new vertices/edges and the properties are an exact match, then mergeV() will return the existing vertex/edge. If not an exact match, you'll get an exception (unless you use the onMatch option to handle those situations). So for example, if I already had a Person vertex with ID 3, name Jane, and address 222 5th Avenue, then merge would just return that vertex. But if I already had a Person vertex with ID 3 and different property values, then this would throw an exception for a vertex that already exists (again, unless you use the onMatch option to handle those cases).
Want results from more Discord servers?
Add your server