How to update a field in an embedded document in orientdb - orientdb

Please I need a orientdb sql query to update a field in an embedded document.
the Documents sample is like this
[
{"id":"fd8ij14uc","text":"Option 1","votes":[]},
{"id":"t44d32z73","text":"Option 2","votes":[]},
{"id":"spceth23q","text":"Option 3","votes":[]}
]
how do I add value like '#12:2' to the "votes" where "id":"fd8ij14uc"

I tried with this structure :
create class Vote
create property Vote.vote integer
create class MyDocument
create property MyDocument.id String
create property MyDocument.text String
create property MyDocument.votes Linklist Vote
create class MyClass
create property MyClass.myField Embedded MyDocument
insert into vote(vote) values (25),(30),(31)
insert into MyClass content{"myField": {"#type":"d", "#class": "MyDocument","id": "fd8ij14uc","text": "Option 1", "votes": []}}
insert into MyClass content{"myField": {"#type":"d", "#class": "MyDocument","id": "t44d32z73","text": "Option 2", "votes": []}}
insert into MyClass content{"myField": {"#type":"d", "#class": "MyDocument","id": "spceth23q","text": "Option 1", "votes": []}}
UPDATE MyClass set myField.votes=[#12:0] WHERE myField.id="fd8ij14uc"

Related

How to structure NoSQL Documents in Azure for Lookup By Array of String contains?

NoSQL newbie here..
I have Employee documents and every Employee has a name and has one to many tags. Here is a possible representation of an employee object in JSON format:
{
"name": "John Doe",
"tags": ["blue", "red", "green"]
}
I want to be able to query Employee instances in Cosmos DB by their tags. For example, I want to find an Employee where tags contains 'green'. An Employee will not have too many tags, maybe up to 10 or 15 at most.
What is the best way to model the document structure for this use case? cosmos db documentation here suggests a structure akin to following for a reason I do not understand:
{
"name": "John Doe",
"tags": [
{
"name": "blue"
},
{
"name": "red"
}
]
}
Is there any reason to split a String array into child JSON objects like this?
How to model documents is totally based on your requirement, there is no strict rule for that.
For your doc structure, I did some test on my side and this all my test doc,4 docs in total:
I can use the query below to find out all employees that contain the "green" tag:
SELECT c.name,c.tags FROM c where ARRAY_CONTAINS(c.tags, "green")

MongoDB Reuse _id

Say I have a simple schema for Users, which gets an automatically generated _id:
{
_id: ObjectId("9dfhdf9fdhd90dfdhdaf"),
name: "Joe Shmoe"
}
And then I have a schema for Groups where I can add Users into a members array.
{
name: "Joe's Group",
members: [{
_id: ObjectId("58fdaffdhfd9fdsahfdsfa"),
name: "Joe Shmoe"
}]
}
Objects within an array get new autogenerated IDs, but I'd like to keep that _id field consistent and reuse it so that the member in the Group has the same attributes as they do in the Users collection.
My obvious solution is to create an independent id field in the members' User object that references their _id in the Users collection, but that seems cluttered having two separate id fields for each member.
My question is, is what I'm attempting bad practice? What's the correct way to add existing objects into a collection from another collection?
I think what you're referring to is : manual or DB References in data modelling.
original_id = ObjectId()
db.places.insert({
"_id": original_id,
"name": "Broadway Center",
"url": "bc.example.net"
})
db.people.insert({
"name": "Erin",
"places_id": original_id,
"url": "bc.example.net/Erin"
})
Check the documentation here

Update nested property in JSONB field in PostgreSQL

I have data structure like:
id bigint
articles jsonb
Example:
id: 1,
articles: {
"1": {
"title": "foo"
},
"2": {
"title": "bar"
}
}
I want to change field name of title (for example articleTitle). Is there any easy way to do that ?
Edit: I can do that with string replace, but can I do that operating on jsonb? Like using jsonb_set() ?
UPDATE person
SET articles = replace(articles::TEXT,'"title":','"articleTitle":')::jsonb

On mongoimport, can I easily map a field to _id on json import?

I need to import a dataset that looks like the similar (abbreviated) dataset.
[
{
"itemId": 1,
"name": "Item",
"qty": "10"
},
...
]
The tricky part is that doing inserts indefinitely will not raise any exception, but the itemId field would represent a valid identifier equivalent to _id field if it could be accepted as such.
Does any option akin to --idField itemId exists?

How to set unique compound index on json array elements?

I have table tags and json column translations inside. This column looks like this:
translations: [
{ text: "Tag1", language: "en-us" },
{ text: "Tag1_cn", language: "zh-cn" },
...
]
Is it possible to set unique index on text+language across all rows? I would like to prevent inserting tag with same text+language to the tags table. So far I was using two tables - tags and tags_translations however I wanted to avoid extra join when querying for tags.
e.g.
CREATE TABLE jsondemo (blah json);
INSERT INTO jsondemo(blah) VALUES ('[
{ "text": "Tag1", "language": "en-us" },
{ "text": "Tag1_cn", "language": "zh-cn" }]');