How to add, edit, check existence of LINK in LINKSET ?

957 views
Skip to first unread message

Gaurav Dhiman

unread,
Mar 27, 2014, 7:04:48 AM3/27/14
to orient-...@googlegroups.com
How to add, edit and check an existence of LINK in LINKSET ?

Considering below case for document based database, can some on give me code example for add, edit and check existence cases.

Lets say

company.employees is LINKSET (array of links to employee objects)
employee.company is LINK (reverse link to company object)

While adding employee, I need to do two way linking (company to employee and employee to company), how to do it ?

var emp = db.getInstance("employee");
emp.field("name", "John");
emp.field("age", "35");
emp.filed("company", "#2:4");     // Linked from employee to company
company.field("employees", ?????);   // How to create link from company to employee ?

Regards,
Gaurav

Gaurav Dhiman

unread,
Mar 27, 2014, 7:06:08 AM3/27/14
to orient-...@googlegroups.com
I mean add, delete (not edit) and check existence.

Best Regards,
Gaurav


--

---
You received this message because you are subscribed to a topic in the Google Groups "OrientDB" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/orient-database/cvYD7oqwkCQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to orient-databa...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Arve Knudsen

unread,
Mar 27, 2014, 11:30:36 AM3/27/14
to orient-...@googlegroups.com
Gaurav, regarding adding to a linkset, you may remember my question on StackOverflow about saving document links with a document. My answer to that question shows how to do this, and it looks like it would solve your example.

Hope this helps,
Arve


--

---
You received this message because you are subscribed to the Google Groups "OrientDB" group.
To unsubscribe from this group and stop receiving emails from it, send an email to orient-databa...@googlegroups.com.

Gaurav Dhiman

unread,
Mar 28, 2014, 8:27:34 AM3/28/14
to orient-...@googlegroups.com
Thanks Arve for response, but I am still not clear.

My queries are simple. I am using OrientDB Javascript functions:

Just for example, if my company.employees = [#4:1, #4:5, #4:3]

1. How can I add #4:6 to linkset ? After addition it should be [#4:1, #4:5, #4:3, #4:6]

2. How can I delete #4:5 from linkset ? After deletion it should be [#4:1, #4:3, #4:6]

3. If I add #4:1 again to linkset, will it check for duplicate and return error or do I need to check problematically before adding to linset ? If I need to do it, how to do it ?

I am sure there must be some methods to add, delete and check existence on linkset & linkmap, I am not just aware of those.
Any pointers will be helpful.

Thanks once again !

Regards,
Gaurav

Gaurav Dhiman

unread,
Mar 28, 2014, 4:15:17 PM3/28/14
to orient-...@googlegroups.com

Can anyone share code example (JavaScript) to do this ?

You received this message because you are subscribed to a topic in the Google Groups "OrientDB" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/orient-database/cvYD7oqwkCQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to orient-databa...@googlegroups.com.

Luca Garulli

unread,
Mar 29, 2014, 1:43:28 PM3/29/14
to orient-database

Gaurav Dhiman

unread,
Mar 31, 2014, 8:39:52 AM3/31/14
to orient-...@googlegroups.com
Thanks Luca, it helped me.
Are there some methods of ODocument object also that can be used to add, delete objects from collections (LINKSET / LINKMAP). Something like below:

comp = db.query("select from company #9:0");
comp = comp[0];
comp.add("employees", #10:4); OR comp.add("employees", emp);  // emp being object / record not @rid
comp.remove("employees", #10:2); OR comp.remove("employees", emp);   // emp being object / record not @rid
comp.save();
db.commit();

Regards,
Gaurav


Nhat Nguyen

unread,
Apr 1, 2014, 7:54:38 AM4/1/14
to orient-...@googlegroups.com
Is there a java example to crud link/linkset/linklist/linkmap into a record<ODocument>?

Regards,
Nhat

Arve Knudsen

unread,
Apr 1, 2014, 1:16:03 PM4/1/14
to orient-...@googlegroups.com
Nhat, my answer on StackOverflow shows how to create an ODocument with a linkset, in Scala. Scala is so close to Java, that you should be able to figure it out. My advice apart from this is to declare in the database schema that your properties are link/linkset etc., so that you'll get an error if you supply the wrong type from Java.

Hth,
Arve

Gaurav Dhiman

unread,
Apr 1, 2014, 1:45:08 PM4/1/14
to orient-...@googlegroups.com
As suggested by Arve, its advisable to define the property as LINK or LINKSET or LINKMAP (whatever). If you do so, simple assignment of one ODocument to property will LINK it, as internally OrientDB seems to understand that its linking in embedding.

Example:

comp = db.getInstance("Company");
comp.field("name","Company name");
comp.save();
db.commit();
for (i= 0; i<5; i++) {
   emp = db.getInstance("Employee");
   emp.field("name", "Employee name");
   emp.field("company", comp);      // saving LINK
   emp.save();
   emps[i] = emp;
}
db.commit();
comp.field("employees", emps);      // saving LINKSET / LINKMAP
comp.save();
db.commit();

Hope it helps.

Best Regards,

Gaurav

Empowering Businesses with Technology

Our Product Offerings:
___________________________________

Nhat Nguyen

unread,
Apr 2, 2014, 9:51:03 AM4/2/14
to orient-...@googlegroups.com
Hey,

great thanks. I am a bit curious couldn´t you have just done one "db.commit()" at the end? 
Maybe i missed something.
Message has been deleted

Kevin Daly

unread,
Oct 22, 2014, 10:39:45 AM10/22/14
to orient-...@googlegroups.com
Is there a way of doing this efficiently with the Document Java API?

I am looking at a possibility to have 180,000,000 links stored in a linkset so not sure if using .field would work.
Reply all
Reply to author
Forward
0 new messages