jj pan
jj pan

Reputation: 307

Gremlin to update meta properties of existing vertex

I am create a vertex with its properties. Each property will have its meta-properties

// Define schema in Gemlin Console

// Create vertex
mgmt.makeVertexLabel('v01').make()

// Create properties
mgmt.makePropertyKey('p01').dataType(String.class).cardinality(LIST).make()

// Create meta-properties
mgmt.makePropertyKey('created_by').dataType(String.class).cardinality(SINGLE).make()
mgmt.makePropertyKey('created_date').dataType(String.class).cardinality(SINGLE).make()
mgmt.makePropertyKey('modified_by').dataType(String.class).cardinality(SINGLE).make()
mgmt.makePropertyKey('modified_date').dataType(String.class).cardinality(SINGLE).make()

// Insert data
p.addV('v01').
    property(list, 'p01', 'pvalue01', 'created_by', 'system01' ,'created_date', new Date(), 'modified_by', '', 'modified_date')

Meta properties info:

created_by: system name who created the property value

created_date: timestamp of created value

modified_by: system name who updated the property value

modified_date: timestamp of updated value

When property value pvalue01 is added to property p01, only created_by and created_date have values while modified_by and modified_date have empty string.

If newer value pvalue02 is added to property p01, how should I update the meta-properties (modified_by and modified_date) for pvalue01?

Upvotes: 1

Views: 407

Answers (1)

Kelvin Lawrence
Kelvin Lawrence

Reputation: 14371

To change the value of a meta property you simply have to locate it and use a property step to change its value. For example

g.V().hasLabel('v01').
    properties('p01').
    property('modified_date',new Date())

If you need to update multiple properties, the value returned from the property step is the Vertex Property, so you can chain them together.

g.V().hasLabel('v01').
    properties('p01').
    property('modified_date',new Date()).
    property('modified_by','Me')

UPDATED to add an example of how this works when list cardinality is in use:

gremlin> g.addV('V01').
           property(list,'mylist','pv1').
           property(list,'mylist','pv2')
==>v[60870]

gremlin> g.V().hasLabel('V01').propertyMap()
==>[mylist:[vp[mylist->pv1],vp[mylist->pv2]]]

gremlin> g.V().hasLabel('V01').properties('mylist')
==>vp[mylist->pv1]
==>vp[mylist->pv2]

gremlin> g.V().hasLabel('V01').properties('mylist').hasValue('pv2')
==>vp[mylist->pv2]

gremlin> g.V().hasLabel('V01').properties('mylist').hasValue('pv2').property('meta',123)
==>vp[mylist->pv2]

gremlin> g.V().hasLabel('V01').properties('mylist').hasValue('pv2').properties()
==>p[meta->123]         

Upvotes: 3

Related Questions