How to insert embedded document in orientdb - orientdb

I have a class etest1 with properties name and text. Then I have a class etest2 with properties lala and etest (the embedded etest1). But I am not able to insert anything.
If I do:
insert into etest2 content {"lala" : "test lala", etest : {"name" : "das", "text" : "dasd"}}
I get an exception: The field 'etest2.etest' has been declared as EMBEDDED but an incompatible type is used. Value: {name=das, text=dasd}
If I do:
insert into etest2 content {"lala" : "test lala", etest : "#17:10"}
I get another exception: The field 'etest2.etest' has been declared as EMBEDDED but the value is the RecordID #17:10
So how can I insert a embedded document. If it is somehow possible I would like something like the first method I tried. I would like more doing just one insert instead of two inserts.

If this is your situation:
create class etest1
create property etest1.name string
create property etest1.text string
create class etest2
create property etest2.lala string
create property etest2.etest embedded etest1
you can do:
insert into etest2 content {"lala" : "test lala", etest : {"#type":"d", "#class":"etest1", "name" : "das", "text" : "dasd"}}

Related

Changing the default MongoDB ObjectID generator

I'm using the mongo-go driver for Go to save some documents on mongoDb. Everyhting works fine, but i'm wondering if there is a way to change how the ID is auto-generated. Right now the document model in the code has the primitive.ObjectID type, is something like this
type Review struct {
ID primitive.ObjectID `json:"id,omitempty" bson:"_id,omitempty"`
Title string `json:"title"`
Text string `json:"text"`
Rate float64 `json:"rate"`
Date time.Time `json:"date"`
Product Product `json:"product"`
}
And the document created is something like this
{
"_id" : ObjectId("5d6f739a20d42db438016cb1"),
"title" : "test-id",
"text" : "rev-text",
"rate" : 5.0,
"date" : ISODate("2019-09-02T12:18:00.000+02:00"),
"status" : "pending"
}
So far so good. However i want the ID to be a UUID, not an ObjectId. I know i can change the struct ID type to UUID or string and set that field when i'm saving the document, however i want to know if there is a way to change the default ObjectID generator of mongoDb so it generates a UUID automatically when saving a new document
You don't have to use an ObjectID for _id:
ID string `json:"id,omitempty" bson:"_id,omitempty"`
Then you can insert any value you want for _id. However, you won't be able to auto-generate ID this way, you have to generate the ID yourself, and insert it with the generated ID.

OrientDB Nested JSON Insert

I have a OrientDB question. For the statement
INSERT INTO Account CONTENT
{ name : 'Luca',
vehicles : {
#class : 'Vehicle',
type : 'Car',
model : 'Maserati',
isItTrue: false
}
}
I get a result (see screenshot), but the class/table 'Vehicle' does not contain any entries. Is this a valid result?
I expected that automatically some values are written into 'Vehicle' and connected to 'Account'. Only the column 'vehicles' from 'Account' contains JSON-Data.
Thank you for your response.
Best regards
Karo
Screenshot OrientDB
With your query you are inserting embedded data into Account, if you want to insert some data into Account and Vehicle, you should do it in different queries and then if you want you can connect them with an edge. See example:
create class Account extends v
create class Vehicle extends v
create class owns extends e
create property Account.name String
create property Vehicle.type String
create property Vehicle.model String
create property Vehicle.isItTrue BOOLEAN
insert into Account(name) values ("Luca")
insert into Vehicle(type, model, isItTrue) values ("Car", "Maserati", false)
create edge owns from (select from Account where name = "Luca") to (select from Vehicle where model = "Maserati")
Alternatively, if you are not looking to create edges and you just want a link from document to document, the below will give you desired results in single insert / transaction while still keeping your json structure for the linked record
create class Account
create class Vehicle
create property Account.vehicles embedded Vehicle
INSERT INTO Account set name = "luca", vehicles = [ {
"#type": "d",
"#class" : "Vehicle",
"type" : "Car",
"model" : "Maserati",
"isItTrue": false
}]

What is the correct way to UPDATE a document in an embedded list?

I am having some trouble finding a way to update, i.e. modify or delete a certain document in an embedded list. Here is my case:
CREATE CLASS Tag EXTENDS V
CREATE PROPERTY Tag.label STRING
CREATE CLASS Profession
CREATE PROPERTY Profession.jobtitle STRING
CREATE PROPERTY Profession.tags LINKSET Tag
CREATE CLASS UserProfile EXTENDS V
CREATE PROPERTY UserProfile.screenname STRING
CREATE PROPERTY UserProfile.profession EMBEDDEDLIST Profession
So, adding an entry to UserProfile.profession is no problem:
UPDATE UserProfile ADD profession =
{"#type":"d","#class":"Profession","jobtitle":"Actress", "tags" : ["#22:5"]}
WHERE screenname = 'emma'
Given some entry 'emma' for UserProfile and a Tag with id #22:5.
However, when I try to update the Profession-document with jobtitle 'Actress', how exactly should I proceed? I tried the following approach, which worked with but one entry in the list only:
UPDATE UserProfile SET profession =
{"#type":"d","#class":"Profession","jobtitle":"Actress", "tags" : ["#22:7", "#22:9"]}
WHERE profession.jobtitle = 'Actress'
AND screenname = 'emma'
This statement throws no exception and returns 0 as number of affected records.
In general: How do I access a specific entry (using a key of the document itself) in an embedded list or set to update or remove it?
Also: is there an easier way to update the tags linkset in the Profession-document in the embedded list? Or do I always have to get the whole document and write a modified version back?
Thanks!
Ingo
You can use
UPDATE UserProfile set profession = [{"#type":"d","#class":"Profession","jobtitle":"Actress", "tags" : ["#22:7", "#22:9"]}]
WHERE profession.jobtitle contains "Actress" AND screenname = 'emma'
UPDATE
You could use this javascript function
var g=orient.getGraph();
var b=g.command("sql","select from userprofile");
var tag1=g.command("sql","select from #22:7");
var tag2=g.command("sql","select from #22:9");
for(i=0;i<b.length;i++){
var screenname= b[i].getProperty("screenname");
if(screenname=="emma"){
var pro=b[i].getProperty("profession");
for(j=0;j<pro.length;j++){
if(pro[j].field("jobtitle")=="Actress"){
pro[j].field("tags",[tag1[0],tag2[0]]);
}
}
b[i].save();
}
}
try this
UPDATE UserProfile SET profession.jobtitle = 'aaaa'
WHERE profession.jobtitle = 'Actress'
AND screenname = 'emma'

json type in Cassandra data model?

I am wondering if I could have json data type in the column family:
My table will have a unique row key, and column name of "tweets_json" and column value of json content.
How would I create such a table in CQL/cassandra or using python Driver or CQLengine?
tweet_json = json.encode({
"tweet_id" : tweet_id,
"body" : tweet_body,
"user_name" : this_user,
"timestamp" : timestamp
})
Basicall
Just have a text column? If you're not looking to filter on the json, then that should be fine. If you are, then you'll need to store it as proper columns, rather than as json.

How can I create a link in orientdb studio?

In this link there is a very basic example.
I have a class, its name is "dog" and it has a "name" property and a "childs" property. The "childs" property type is LINKLIST.
When a try to create a new dog object I crete a "childs" field but I don't know how to fill it.
Please point me to a more complete tutorial (actualy I search it with out success).
You can use the query for that:
insert into animal set name = 'dog', children = [<rid>]
where <rid> is the record id of the child.