我是OrientDB的新手,我正在开发一个供内部使用的测试应用程序(orientdb 2.2.17)。
我正在尝试将一个新文档添加到类中,并将子查询作为其中一个值。
类:
CREATE CLASS Website
CREATE PROPERTY Website.id String
CREATE PROPERTY Website.name String
CREATE PROPERTY Website.created DateTime
CREATE CLASS User
CREATE PROPERTY UserEndpoint.id String
CREATE PROPERTY UserEndpoint.website String
CREATE PROPERTY UserEndpoint.created DateTime
我正在尝试添加一个引用某个网站的新用户。
例如:
INSERT INTO Website (name) VALUES ('localhost:8090')
然后添加用户:
INSERT INTO User (website) VALUES (SELECT id FROM Website WHERE )
OR
INSERT INTO User (website) VALUES (SELECT @rid FROM Website WHERE )
结果不是我所期望的,User.website
参数是表示数组的字符串,而不是表示单个值的字符串。
{
"result": [
{
"@type": "d",
"@rid": "#30:0",
"@version": 1,
"@class": "User",
"created": "2017-12-28T16:35:31",
"id": "a9816adc-8808-4673-b5a7-b11493b0ce36",
"website": "[Website#25:0{created:Thu Dec 28 16:34:25 UTC 2017,id:e0ce0e68-003b-4a01-8c4f-856d9eeb6125,name:localhost:56} v1]"
}
]
"notification": "..." }
预期:
{
"result": [
{
"@type": "d",
"@rid": "#30:0",
"@version": 1,
"@class": "User",
"created": "2017-12-28T16:35:31",
"id": "a9816adc-8808-4673-b5a7-b11493b0ce36",
"website": "#25:0"
}
]
"notification": "..." }
我还想在网站和用户之间建立关系(用户属于一个网站,网站可能有很多用户),这是正确的方法:
CREATE LINK websiteUsers TYPE LINKSET FROM User.website TO Website.rid
当我查询用户并且我想获取网站详细信息时,是否自动添加到结果中?
答案 0 :(得分:0)
您必须将website
属性创建为链接,而不是字符串:
CREATE PROPERTY UserEndpoint.website LINK Website
这将导致您可以查询的实际链接。
作为替代方案,您可以使用边缘(图形模型)替换链接
CREATE CLASS Website EXTENDS V
CREATE CLASS User EXTENDS V
CREATE CLASS HasWebsite EXTENDS E
INSERT INTO User (name) VALUES ('foo')
INSERT INTO Website (name) VALUES ('localhost:8090')
CREATE EDGE HasWebsite FROM (SELECT FROM User WHERE name = 'foo') TO (SELECT FROM Website WHERE name = 'localhost:8090')
然后按如下方式查询:
get the website for a user:
SELECT expand(out("HasWebsite")) FROM User WHERE name = ?