在图数据库中存储模型和关系

时间:2016-03-30 09:53:01

标签: ruby-on-rails neo4j orientdb

我有以下型号:

Person (:age, :gender)
Company (:name, address)
Products (:price, :name)

一个人可以拥有多家公司,也可以是多家公司的客户。公司可以生产多种产品,但也可以购买其他公司生产的产品。

如何使用OrientDB和Neo4J来表示这一点?如何在Rails中查询这些模型/什么是宝石方面的最佳选择?

示例查询:查找30至40岁之间由人所有的公司制造的特定价格范围之间的所有产品(假设人具有属性:年龄),其中大多数人的年龄在20至30岁之间作为客户。

注意:我还没有决定是否将OrientDB或Neo4j用于应用程序,或者我可能会将Neo4j用于我的应用程序的开源部分处理的数据块....... < / p>

2 个答案:

答案 0 :(得分:3)

在Orient中你的模型应该是这样的:

enter image description here

在这里创建它是要运行的命令:

create class Person extends V
create property Person.age integer
create property Person.gender string

create class Company extends V
create property Company.name string
create property Company.address string

create class Products extends V
create property Products.price integer
create property Products.name string

create class own extends E
create class costumer extends E
create class manufacture extends E
create class purchase extends E
create class manufactured extends E

insert into Person(age,gender) values (20,"M")
insert into Company(name,address) values ("company_01","address_01"),("company_02","address_02"),("company_03","address_03"),("company_04","address_04")
insert into Products(price,name) values (200,"product_01"),(750,"product_02"),(90,"product_03"),(368,"product_04"),(112,"product_05")

create edge own from #12:0 to #13:0
create edge own from #12:0 to #13:1

create edge customer from #13:2 to #12:0
create edge customer from #13:3 to #12:0

create edge manufacture from #13:2 to #14:0
create edge manufacture from #13:2 to #14:1
create edge manufacture from #13:2 to #14:2

create edge purchase from #13:2 to #14:4

create edge manufacture from #13:0 to #14:4
create edge manufacture from #13:0 to #14:0

答案 1 :(得分:1)

  

一个人可以拥有多家公司,也可以是一个客户   几家公司。公司可以生产多种产品但可以   还购买其他公司生产的产品。

要对其进行建模,您应该使用实体之间的标签和关系:

在你的情况下,它可能就像

(p:Person)-[:OWNED_BY]->(c:Company),
(p:Person)-[:IS_CUSTOMER]->(c:Company),
(c:Company)-[:MANUFACTURED_BY]->(pr:Product),
(c:Company)-[:USES]->(pr:Product)

您可以像

一样查询
Match (p:Products) where p.price < 2000 and p.price > 1000 with p
Match (p)<-[MANUFACTURED_BY]-(c:Company)<-[:OWNED_BY]-(owner:Person)
where owner.age < 40 and owner.age > 30 with p, c, owner
Match (c)<-[:IS_CUSTOMER]-(employee:Person) 
where employee.age < 30 and employee.age > 20 
with p, c, count(employee) as midYeareEmployee ...