How can I query a Vertex by multiple edges

Hi, I created this data layout:

await db.class.create("Product", "V")
await db.class.create("Option", "V")

const product1 = await db.create("VERTEX", "Product").set({ name: "Product 1" }).one()
const product2 = await db.create("VERTEX", "Product").set({ name: "Product 2" }).one()
const product3 = await db.create("VERTEX", "Product").set({ name: "Product 3" }).one()

const option1 = await db.create("VERTEX", "Option").set({ name: "Option 1" }).one() // rid: #25:0
const option2 = await db.create("VERTEX", "Option").set({ name: "Option 2" }).one() // rid: #26:0
const option3 = await db.create("VERTEX", "Option").set({ name: "Option 3" }).one() // rid: #27:0

await db.create("EDGE", "HasValue").from(product1["@rid"]).to(option1["@rid"]).one()
await db.create("EDGE", "HasValue").from(product1["@rid"]).to(option3["@rid"]).one()

await db.create("EDGE", "HasValue").from(product2["@rid"]).to(option1["@rid"]).one()

await db.create("EDGE", "HasValue").from(product3["@rid"]).to(option2["@rid"]).one()
await db.create("EDGE", "HasValue").from(product3["@rid"]).to(option3["@rid"]).one()

Now I would like to find all products that have an HasValue Edge with "#25:0" OR "#26:0" AND HasValue Edge with "#27:0"
=> This filter shoud return Product 1 and Product 3

Here is how I would write it in SQL with n-m relation table :

 SELECT count(*) from "products" 
 join "HasValue" as v1 on "v1"."productId" =  "products"."id" and "v1"."optionId" in ('#25:0', '#26:0' )
 join "HasValue" as v2 on "v2"."productId" =  "products"."id" and "v2"."optionId" in ('#27:0')

How can I do this query in OrientDB ? Thx

Hi,
try this:
select from Product where out("HasValue").@rid contains "#27:0" AND (out("HasValue").@rid contains "#25:0" OR out("HasValue").@rid contains "#26:0")

Hope it helps.

Regards,
Michela

1 Like

Thx Michela! This worked :boom:

Hi @Johnson

Iā€™d also suggest to have a look at MATCH syntax, it could help a lot https://orientdb.com/docs/3.0.x/sql/SQL-Match.html

Thanks

Luigi