Joining on array with RethinkDB - inner-join

I'm having some issues with joining two tables.
I've two tables:
Qas
[{
"course": "swd" ,
"id": "c9b2e8cb-15f9-4f93-b677-6dff2880d383" ,
"number": 1 ,
"questions": [
{
"date": "Wednesday, December 16th, 2015, 11:09" ,
"owner": 4362445 ,
"question": "Could you explain promises?" ,
"questionid": "2766d4bf-fd79-4f94-8d22-788d4e6b89c2" ,
}
] ,
"session": 1 ,
"unit": 1
}]
Users
[{
"avatar_url": https://avatars.githubusercontent.com/u/4362445?v=3, »
"displayName": "Jamie" ,
"id": 4362445 ,
"oauth_token": "46451371bffea867a71b9cc357eff4fd9a06591e" ,
"role": "student" ,
"team": {
"id": 1729535 ,
"name": "dwa-group-a"
} ,
"username": "Jamiek94"
}]
Now i'd like to join (qas -> questions - > owner) on (users -> id) and finally zip them togheter.
So the result would look like this:
[{
"course": "swd" ,
"id": "c9b2e8cb-15f9-4f93-b677-6dff2880d383" ,
"number": 1 ,
"questions": [
{
"date": "Wednesday, December 16th, 2015, 11:09" ,
"owner": 4362445 ,
"question": "Could you explain promises?" ,
"questionid": "2766d4bf-fd79-4f94-8d22-788d4e6b89c2" ,
"user" : {
"avatar_url": https://avatars.githubusercontent.com/u/4362445?v=3, »
"displayName": "Jamie" ,
"id": 4362445 ,
"oauth_token": "46451371bffea867a71b9cc357eff4fd9a06591e" ,
"role": "student" ,
"team": {
"id": 1729535 ,
"name": "dwa-group-a"
}
}
] ,
"session": 1 ,
"unit": 1
}]
The query i am using is:
r.db('GitSmurf').table('qa').eqJoin(r.row('questions')('owner'), r.db('GitSmurf').table('users'), { index : 'id'})
This results in:
No results were returned for this query

your questions is an array so you cannot use r.row('questions')('owner')
We can scratch eqJoin and just join manually with map. Something like this should work for you:
r.table('Qas').merge(function(qa) {
return {
questions: qa('questions').map(function(q) {
return q.merge({user: r.table('Users').get(q('owner'))})
})
}
})

Related

Create mulitple records from array-of-arrays within Postgres JSON object

Within my Postgres database, I have a column (type=JSONB) that contains an API response that represents someone response to a Survey Monkey survey. Within this JSON object, there is an array called pages that contains at least 1 item, and something more than 1 item. Then within pages, there is another array called questions. This array also contains at least 1 item, and more often more than 1 item.
I am trying to write a Postgres query to extract the responses from all the questions seen within pages and questions. The final format should be 1 record per question. If the survey has 5 questions, that would create 5 rows. If the survey has 100 questions, across 5 pages, that would mean 100 rows.
+────────────+────────────────────────────────────────────+──────────────────────+
| id | heading | simple_text |
+────────────+────────────────────────────────────────────+──────────────────────+
| 775249149 | I am confident that I am the best. | Somewhat Agree |
| 775249153 | I currently have the skills to be amazing | Somewhat Agree |
| 775249166 | How long until I win the lottery? | 6 to 12 months more |
+────────────+────────────────────────────────────────────+──────────────────────+
I've been trying to use the function json_array_elements, but I'm experiencing some errors when trying to pass in an array-of-arrays.
{
"id": "12345",
"href": "https://api.surveymonkey.com",
"pages": [
{
"id": "142250690",
"questions": [
{
"id": "775249149",
"family": "matrix",
"answers": [
{
"row_id": "5133514018",
"choice_id": "5133514023",
"simple_text": "Somewhat Agree",
"choice_metadata": {
"weight": "5"
}
}
],
"heading": "I am confident that I am the best.",
"subtype": "rating"
},
{
"id": "775249153",
"family": "matrix",
"answers": [
{
"row_id": "5133514112",
"choice_id": "5133514117",
"simple_text": "Somewhat Agree",
"choice_metadata": {
"weight": "5"
}
}
],
"heading": "I currently have the skills to be amazing",
"subtype": "rating"
},
{
"id": "775249166",
"family": "matrix",
"answers": [
{
"row_id": "5133514278",
"choice_id": "5133514280",
"simple_text": "6 to 12 months more",
"choice_metadata": {
"weight": "2"
}
}
],
"heading": "How long until I win the lottery?",
"subtype": "rating"
}
]
}
],
"survey_id": "123456789",
"total_time": 29,
"date_created": "2022-07-25T23:08:36+00:00",
"recipient_id": "",
"date_modified": "2022-07-25T23:09:06+00:00",
"email_address": "",
"collection_mode": "default",
"response_status": "completed",
}
Try to use json_array_elements() twice:
http://sqlfiddle.com/#!17/9eecb/94455
select v->'id', v->'heading', v->'subtype', v->'answers'->0->'simple_text' from (
select json_array_elements(value->'questions')::jsonb as v from json_array_elements('{
"id": "12345",
"href": "https://api.surveymonkey.com",
"pages": [
{
"id": "142250690",
"questions": [
{
"id": "775249149",
"family": "matrix",
"answers": [
{
"row_id": "5133514018",
"choice_id": "5133514023",
"simple_text": "Somewhat Agree",
"choice_metadata": {
"weight": "5"
}
}
],
"heading": "I am confident that I am the best.",
"subtype": "rating"
},
{
"id": "775249153",
"family": "matrix",
"answers": [
{
"row_id": "5133514112",
"choice_id": "5133514117",
"simple_text": "Somewhat Agree",
"choice_metadata": {
"weight": "5"
}
}
],
"heading": "I currently have the skills to be amazing",
"subtype": "rating"
},
{
"id": "775249166",
"family": "matrix",
"answers": [
{
"row_id": "5133514278",
"choice_id": "5133514280",
"simple_text": "6 to 12 months more",
"choice_metadata": {
"weight": "2"
}
}
],
"heading": "How long until I win the lottery?",
"subtype": "rating"
}
]
},
{
"id": "142250690",
"questions": [
{
"id": "775249199",
"family": "matrix",
"answers": [
{
"row_id": "5133514278",
"choice_id": "5133514280",
"simple_text": "6 to 12 months more",
"choice_metadata": {
"weight": "2"
}
}
],
"heading": "qweqweqweqwqqqq?",
"subtype": "rating"
}
]
}
],
"survey_id": "123456789",
"total_time": 29,
"date_created": "2022-07-25T23:08:36+00:00",
"recipient_id": "",
"date_modified": "2022-07-25T23:09:06+00:00",
"email_address": "",
"collection_mode": "default",
"response_status": "completed"
}'::jsonb::json->'pages')
) t;
UPDATE
If you need to process data from table contained described json rows, you need something like this:
http://sqlfiddle.com/#!17/16b65/1

How to get a value in one mongodb collection and use that value to update another document in another collection order & inventory system

Hello I have been stuck for weeks trying to figure how to create a order & inventory system for a project I am working on. I don't know how to properly ask this but my problem is when a user adds items to their cart > I store the order details in a orders collection in mongodb > I then need to figure out how to subtract the quantity of the items in a customers order from my inventory collection. How can I do this with mongodb, Python
This is the document created when a customer places an order
{
"_id": "5eca94b4f56331fd9eade681",
"ordernumber": 343,
"order": {
"order_details": [
{
"customer_info": [
{
"first_name": "John",
"last_name": "Doe",
"email": "email#email.com"
}
],
"shipping_details": [
{
"shipping_address": "Test Address",
"shipping_zip": "12345",
"shippingl_city": "Test city",
"shipping_country": "USA"
}
],
"products_ordered": [
{
"variant_id": "a",
"product_name": "red shirt",
"price": 30,
"quantity": 2,
"image": "imageurl",
"size": "Small"
},
{
"variant_id": "f",
"product_name": "Blue Jeans",
"price": 20,
"quantity": 3,
"image": "imageurl",
"size": "Large"
}
]
}
]
}
}
These are the products in my inventory collection I want inventory order quantity subtracted by the quantity a customer purchased
{
"_id": "5eca0ff4898b8f30a9fee5e5",
"product_id": 1,
"product_name": "red shirt",
"category": "shirts",
"price": 30,
"status": "instock",
"description": "nice red shirt",
"alt": "string",
"images": [
"imageUrl1",
"imageUrl2"
],
"variants": [
{
"Small": [
{
"variant_id": "a",
"inventory": 30
}
],
"Medium": [
{
"variant_id": "b",
"inventory": 10
}
],
"Large": [
{
"variant_id": "c",
"inventory": 10
}
]
}
]
}
{
"_id": "5eca108f898b8f30a9fee5e6",
"product_id": 2,
"product_name": "blue jeans",
"category": "jeans",
"price": 20,
"status": "instock",
"description": "nice blue jeans",
"alt": "string",
"images": [
"ImageURL"
],
"variants": [
{
"Small": [
{
"variant_id": "d",
"inventory": 100
}
],
"Medium": [
{
"variant_id": "e",
"inventory": 150
}
],
"Large": [
{
"variant_id": "f",
"inventory": 70
}
] }
]
}
I would suggest to do it along with the service which creates the order.
I would also like to suggest to refactor the db structure a bit as it would be harder to maintain this in a larger scale.
Because currently we would have to write something like
for ordered_product in products_ordered:
query = { "product_name": ordered_product.get("product_name") }
inventory_product = inventory_collection.find_one(query)
product_id = inventory_product["_id"]
existing_count = inventory_product["variants"][0][ordered_product.size][0]["inventory"]
inventory_product["variants"][0][ordered_product["size"]][0]["inventory"] = existing_count - ordered_product["quantity"]
inventory_collection.update_one({ "_id": product_id }, { "$set": inventory_product })
I have hardcoded the index values of the list. You could use filter() to filter out the variant and size you need.
This code definitely seems messy to me.
Of course you could refactor this code by splitting it into functions inside the model file itself, but I would suggest to refactor the db structure for better scalability.
May be you could move the variants to a seperate collection and use the product_id as a link. You have to think this through before getting on with the code.
Hope this helps.

Deleting Object from Nested Array MongoDB

I am trying to delete the object with the id: "2019-08-22T04:53:11.357Z" from this users portfolio array.
I have searched for the correct query filter to help me delete the object but nothing seems to be working!
Here is the data we are working with. There is currently only one user
(InStock) in the system but more will be added in the future.
[
{
"id": "MTg4MDU3ODM2MDk2NDU0NjU3",
"name": "InStock",
"balance": 7760,
"portfolio": [
{
"id": "2019-08-22T04:15:22.998Z",
"name": "Jordan Shoe",
"size": "10.5",
"price": 150
},
{
"id": "2019-08-22T04:36:37.836Z",
"name": "Nike Tee",
"size": "M",
"price": 35
},
{
"id": "2019-08-22T04:53:11.357Z",
"name": "Adidas Shoe",
"size": "8.5",
"price": 100
}
],
"history": [
]
}
]
and here is what I was trying using what I've seen in other solutions.
db.collection(collectionName).updateOne({"id": "MTg4MDU3ODM2MDk2NDU0NjU3"}, {$pull : {"portfolio": {"id": "2019-08-22T04:36:37.836Z"}}})
I am looking for the line to remove the Adidas Shoe object from InStock's portfolio array.
Try this query:
db.test1.updateOne({"_id" : ObjectId("5d5e7a291b761bfc0420e580")},
{$pull: {"portfolio": {"name": "Adidas Shoe"}}} )
After execution:
{
"_id" : ObjectId("5d5e7a291b761bfc0420e580"),
"portfolio" : [
{
"id" : "2019-08-22T04:15:22.998Z",
"name" : "Jordan Shoe",
"size" : "10.5",
"price" : 150.0
},
{
"id" : "2019-08-22T04:36:37.836Z",
"name" : "Nike Tee",
"size" : "M",
"price" : 35.0
}
],
"name" : "InStock",
"balance" : 7760.0
}
It's Working Fine for you.
db.collection(collectionName).updateOne({"id": "MTg4MDU3ODM2MDk2NDU0NjU3"},
{ $pull: { portfolio: { $elemMatch: { id:"2019-08-22T04:36:37.836Z"} }},

Is there a way to deliver an array via a REST-Webservice in Denodo?

I´m importing a JSON-Datasource in Denodo which contains 2 arrays. In order to work with the data i flatten those arrays. However when delivering the data I want to get back to the initial array structure to get something like
{
"name": "name_of_my_view",
"elements": [
{
"result": [
{
"id": 40033495,
"first_name": Max,
"last_name": Mustermann
},
{
"id": 39960791,
"first_name": "Markus",
"last_name": "Markwart"
}
],
"took_ms": 4,
"result_count": 323,
"errors": [
{}
]
}
],
"links": [
{
"rel": "self",
"href": "https://address"
}
]
}
I have flattend both arrays (result, errors) in order to edit the respective fealds within them. However i only see the option of using UNION to combine them. If i do so I end up having all fealds in one hierarchy like (Ignore the sorting in this example) Oh and note that "code" and "description" are within the "error" array and are not shown in the above example because there are no errors in it:
{
"name": "name_of_my_view",
"elements": [
{
"took_ms": 4,
"result_count": 323,
"code": null,
"description": null,
"id": null,
"first_name": null,
"last_name": null
},
{
"took_ms": 4,
"result_count": 323,
"code": null,
"description": null,
"id": 40033495,
"first_name": null,
"last_name": null
}
],
"links": [
{
"rel": "self",
"href": "https://address"
}
]
}

How to return a filtered nested array in RethinkDB?

Is it possible to filter the elements of a nested array in RethinkDB, similar to $elemMatch in MongoDB?
For example, if we have a simple table collection like:
[
{
"name": "test1" ,
"nested": [
{
"name": "nested1" ,
"user": "paul"
} ,
{
"name": "nested2" ,
"user": "paul"
} ,
{
"name": "nested3" ,
"user": "dave"
}
]
} ,
{
"name": "test2" ,
"nested": [
{
"name": "nested4" ,
"user": "dave"
} ,
{
"name": "nested5" ,
"user": "paul"
} ,
{
"name": "nested6" ,
"user": "steve"
}
]
}
]
How would I write a filter to return documents, with filtered arrays that contain the user "paul"?
So my result would be:
[
{
"name": "test1" ,
"nested": [
{
"name": "nested1" ,
"user": "paul"
} ,
{
"name": "nested2" ,
"user": "paul"
}
]
} ,
{
"name": "test2" ,
"nested": [
{
"name": "nested5" ,
"user": "paul"
}
]
}
]
I can get the documents with:
r.db('test').table('example').filter(r.row('nested')('user').contains('paul'))
How do I then filter the nested arrays?
Thanks,
Paul
table.map(function(row) {
return row.merge({
nested: row('nested').filter({user: 'paul'})
})
})

Resources