RethinkDB - get range of values inside nested arrays - rethinkdb

I am new to RethinkDB, and am working with a data set with rows like the below:
{
"data": {
"items": [
{
"name: "Foo",
"value": 20
},
{
"name: "Bar",
"value": 70
}
]
}
}
I would like to run a query to return the range of item values in the entire dataset, for which the name is "Foo".
Any help is appreciated.

You could write [row('data')('items')('value').min(), row('data')('items')('value').max()].

Related

Dataweave 2 transforming an array of objects having a id field, into a object whose elements are maps

need some suggestion for a dataweave 2 transformation that can transform the following input (Array of Object having an id field) to the following output (grouping in an array the objects sharing the same id into a map, having the id as key).
Input :
[
{
"id": "1117",
"Tot": "10.0",
"Per": "7/2025"
},
{
"id": "1117",
"Tot": "200.0",
"Per": "2/2021"
},
{
"id": "7997",
"Tot": "78.0",
"Per": "10/2023"
}
]
output
{
"1117": [
{
"id": "1117",
"Tot": "10.0",
"Per": "7/2025"
},
{
"id": "1117",
"Tot": "200.0",
"Per": "2/2021"
}
],
"7997": [
{
"id": "7997",
"Tot": "78.0",
"Per": "10/2023"
}
]
}
Any idea?
Thanks
%dw 2.0
output application/json
---
payload groupBy $.id
You just want to group by the ID? There ya go.
I would recommend going to https://developer.mulesoft.com/learn/dataweave and then going to the tutorial tab (top right). It will walk through these basic scenarios :)
payload groupBy $.id
Adding more chars to reach 30.

Incorrectly selected data in the query

Only articles that contain the EmailMarketing tag are needed.
I'm probably doing the wrong search on the tag, since it's an array of values, not a single object, but I don't know how to do it right, I'm just learning graphql. Any help would be appreciated
query:
query {
enArticles {
title
previewText
tags(where: {name: "EmailMarketing"}){
name
}
}
}
result:
{
"data": {
"enArticles": [
{
"title": "title1",
"previewText": "previewText1",
"tags": [
{
"name": "EmailMarketing"
},
{
"name": "Personalization"
},
{
"name": "Advertising_campaign"
}
]
},
{
"title": "title2",
"previewText": "previewText2",
"tags": [
{
"name": "Marketing_strategy"
},
{
"name": "Marketing"
},
{
"name": "Marketing_campaign"
}
]
},
{
"title": "article 12",
"previewText": "article12",
"tags": []
}
]
}
}
I believe you first need to have coded an equality operator within your GraphQL schema. There's a good explanation of that here.
Once you add an equality operator - say, for example _eq - you can use it something like this:
query {
enArticles {
title
previewText
tags(where: {name: {_eq: "EmailMarketing"}}){
name
}
}
}
Specifically, you would need to create a filter and resolver.
The example here may help.

GraphQL filtering an array

I am using GatsBy GraphiQL to write a query to return a single element, but the query returns all of the elements.
Here is my testing data:
{
"data": {
"mKT": {
"data": [
{
"name": "Apple",
"description": "apple's desc"
},
{
"name": "Orange",
"description": "orange's desc"
},
{
"name": "Banana",
"description": "banana's desc"
}
]
}
},
"extensions": {}
}
And, here is the GraphQL query:
query MyQuery {
mKT(data: {elemMatch: {name: {eq: "Apple"}}}) {
data {
name
description
}
}
}
I expect to get:
{
"name": "Apple",
"description": "apple's desc"
}
However, running the query returns all of the data.
Any idea how to fix this issue?
Simply use:
query MyQuery {
mKT(data: {filter: {name: {eq: "Apple"}}}) {
data {
name
description
}
}
}
The filter keyword should do the trick.
Check the GraphQL Reference (in Gatsby docs) for further details.
I believe this is the same case as:
GraphQL query to access first item in an array?
You cannot use filters, limits, etc to an structure if it have not been defined on the source.
If you have access to the source, add something like limit to the field data.
If not, you will get the full array, and you have to process it on your side.

Strapi GraphQL query: "start" argument wouldn't work

I am running into a very strange problem with my queries in Strapi (version 3.0.0-alpha.26.2). I have a users collection with 3 documents that I'm trying to fetch via GraphQL. To fetch all users the query is:
users {
firstName
}
This returns the following:
{
"data": {
"users": [
{
"firstName": "Arnold"
},
{
"firstName": "Bill"
},
{
"firstName": "Vin"
}
]
}
}
3 names. Now, say, I wished to retrieve only the first 2 users. For such pagination use-cases, there's two arguments one could pass in a Strapi query: start (defines the index to start at) and limit (defines the number of elements to return). So now the query would be:
users(start: 0, limit: 2) {
firstName
}
This returns the first two names as expected:
{
"data": {
"users": [
{
"firstName": "Arnold"
},
{
"firstName": "Bill"
}
]
}
}
But what if I want the last 2 users here, i.e. Bill and Vin? Should be as straightforward as:
users(start: 1, limit: 2){
firstName
}
But this still returns Arnold and Bill, while you'd expect the following:
{
"data": {
"users": [
{
"firstName": "Bill"
},
{
"firstName": "Vin"
}
]
}
}
No matter what value I use for start, it always starts at the 0th item. You could do start: 200 (when there are only 3 items in the users collection) and it'd still return the exact same result! What sorcery is this??
The issue can be reproduced at https://dev.schandillia.com/graphql.

Update a subdocument list object value in RethinkDB

{
"id": 1,
"subdocuments": [
{
"id": "A",
"name": 1
},
{
"id": "B",
"name": 2
},
{
"id": "C",
"name": 3
}
]
}
How do update a subdocument "A"s "name" to a value of 2 in RethinkDB in either Javascript or Python?
If you can rely of the position of your "A " element you can update like this:
r.db("DB").table("TABLE").get(1)
.update({subdocuments:
r.row("subdocuments").changeAt(0, r.row("subdocuments").nth(0).merge({"name":2}))})
If you can not rely on the position, you have to find it yourself:
r.db("DB").table("TABLE").get(1).do(function(doc){
return doc("subdocuments").offsetsOf(function(sub){return sub("id").match("A")}).nth(0)
.do(function(index){
return r.db("DB").table("TABLE").update({"subdocuments":
doc("subdocuments").changeAt(index, doc("subdocuments").nth(index).merge({"name":2})) })})
})
As an alternative you can use the map function to iterate over the array elements and update the one that matches your condition
r.db("DB").table("TABLE").get(1)
.update({
subdocuments: r.row("subdocuments").map(function(sub){
return r.branch(sub("id").eq("A"), sub.merge({name: 2}), sub)
})
})

Resources