在图形数据库中存储模型和关系
Storing models and relationships in a graph database
我有以下型号:
Person (:age, :gender)
Company (:name, address)
Products (:price, :name)
一个人可以拥有几家公司,也可以是几家公司的客户。一家公司可以生产多种产品,但也可以购买其他公司生产的产品。
这如何使用 OrientDB 和 Neo4J 来表示,我如何在 Rails/what 中查询此类模型将是 gems 的最佳选择?
示例查询:查找由年龄在 30 到 40 岁之间的人拥有的公司生产的特定价格范围内的所有产品(假设人具有属性:年龄),这些人的大多数人在 20 到 30 岁之间作为客户的年龄。
注意:我还没有决定是否将 OrientDB 或 Neo4j 用于该应用程序,或者我将使用 Neo4j 用于由我的应用程序的开源部分处理的数据块…… .
A Person can own several Companies and can also be a customer of
several Companies. A Company can manufacture several Products but can
also purchase Products manufactured by other Companies.
要对其建模,您应该使用实体之间的标签和关系:
在你的情况下它可能像
(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 ...
在东方你的模型应该是这样的:
要在此处创建它是 运行:
的命令
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
我有以下型号:
Person (:age, :gender)
Company (:name, address)
Products (:price, :name)
一个人可以拥有几家公司,也可以是几家公司的客户。一家公司可以生产多种产品,但也可以购买其他公司生产的产品。
这如何使用 OrientDB 和 Neo4J 来表示,我如何在 Rails/what 中查询此类模型将是 gems 的最佳选择?
示例查询:查找由年龄在 30 到 40 岁之间的人拥有的公司生产的特定价格范围内的所有产品(假设人具有属性:年龄),这些人的大多数人在 20 到 30 岁之间作为客户的年龄。
注意:我还没有决定是否将 OrientDB 或 Neo4j 用于该应用程序,或者我将使用 Neo4j 用于由我的应用程序的开源部分处理的数据块…… .
A Person can own several Companies and can also be a customer of several Companies. A Company can manufacture several Products but can also purchase Products manufactured by other Companies.
要对其建模,您应该使用实体之间的标签和关系:
在你的情况下它可能像
(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 ...
在东方你的模型应该是这样的:
要在此处创建它是 运行:
的命令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