I have following query which returns the number of request for each status -
r.db.table("request").group("status").count()
result -
[
{
"group": "ACCEPTED",
"reduction": 1
},
{
"group": "DECLINED",
"reduction": 1
},
{
"group": "PENDING",
"reduction": 1
}
]
How can I convert this result to the following using rethinkdb query (javascript)?
{
"ACCEPTED": 1,
"DECLINED": 1,
"PENDING": 1
}
Currently, I am achieving this by iterating the result in api side. But I want this transformation in rethinkdb, if its at all possible?
r.db("test").table("request").group("status").count()
.ungroup()
.map(function(x){ return [x('group'), x('reduction')]; })
.coerceTo("object")
When you want to continue working on your return object you need to
call the ungroup() function first
Then you need to use the map
function to transform your result to:
[ [ "ACCEPTED" , 1 ] , [ "DECLINED" , 1 ] , [ "PENDING" , 1 ] ]
at last you simply transform this to a json-object with coerceTo("object"), which returns you the desired format:
{
"ACCEPTED": 1,
"DECLINED": 1,
"PENDING": 1
}
Related
I have nested array of objects.
How can I filter by specific value in array? I want filter by reg = region1 and value greater than 2
{
"region": [
{
"reg": "region1",
"val": 2
},
{
"reg": "region2",
"val": 6
}
]
}
maybe you can help me find out a query. I do a query Like
$Customers = Customer::paginate();
return response()->json([$Customers]);
this returns (just part of response)
[
{
"current_page": 1,
"data": [{"id": 1,}]
}
]
If I do join with other table all data is just put into "data" output. What I want is this for my join: How to get this output??
[
{
"current_page": 1,
"data": [{
"id": 1,
"join": [{ "join_column" : 1 }],
}]
}
]
Query:
SELECT value FROM test.json_test FORMAT JSON
Response:
{
"meta":
[
{
"name": "value",
"type": "Int32"
}
],
"data":
[
{
"value": 1
}
],
"rows": 1,
"statistics":
{
"elapsed": 0.112135109,
"rows_read": 1,
"bytes_read": 4
}
}
How to exclude unnecessary fields and leave only data field?
You can exclude "statistics" from output:
set output_format_write_statistics=0;
ubuntu-16gb-nbg1-1 :) select 1 format JSON;
SELECT 1
FORMAT JSON
{
"meta":
[
{
"name": "1",
"type": "UInt8"
}
],
"data":
[
{
"1": 1
}
],
"rows": 1
}
1 rows in set. Elapsed: 0.003 sec.
It looks like JSONEachRow cannot be used straightforward because the result JSON is not valid.
You can play with grouping data to the array to get valid JSON:
SELECT
groupArray(value) AS values,
groupArray((value, name)) AS objects
FROM
(
SELECT
1 AS value,
'str1' AS name
UNION ALL
SELECT
2 AS value,
'str2' AS name
)
FORMAT JSONEachRow
/* Result:
{"values":[1,2],"objects":[[1,"str1"],[2,"str2"]]}
*/
where you try running your query?
maybe you can try filter this output via pipelines and JQ
clickhouse-client -q "SELECT value FROM test.json_test FORMAT JSON" | jq .data
{
"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)
})
})
I have a query that uses group() function:
...group('a','b','c','na').count()
the now the result is returns like in the form of group and reduction like this:
How can I get result without group and reduce in the form of
{
"na": 1285
"c" : 487
"b" : 746
"a" : 32
}
I'm not sure, but I think you're misunderstanding what group does.
The group command takes a property and groups documents by that property. So, for example, if you wanted to group documents by the a property, that would look something like this:
{
a: 1
}, {
a: 1
}, {
a: 1
}, {
a: 2
}
Then you would run the following query:
r.table(...).group('a').count().ungroup()
Which would result in:
[
{
"group": 1 ,
"reduction": 3
},
{
"group": 2 ,
"reduction": 1
}
]
By passing multiple arguments to group you are telling it to make distinct groups for all those properties. So you you have the following documents:
[ {
a: 1, b: 1
}, {
a: 1, b: 1
}, {
a: 1, b: 2
}, {
a: 2, b: 1
}]
And you group them by a and b:
r.table(...).group('a', 'b').count().ungroup()
You will get the following result:
[{
"group": [ 1 , 1 ] ,
"reduction": 2
},
{
"group": [ 1 , 2 ] ,
"reduction": 1
},
{
"group": [ 2 , 1 ] ,
"reduction": 1
}]
Your Answer
So, when you do .group('a','b','c','na').count(), you're grouping them by those 4 properties. If you want the following result:
{
"na": 1285
"c" : 487
"b" : 746
"a" : 32
}
Then your documents should look something like this:
[{
property: 'a'
}, {
property: 'c'
}, {
property: 'na'
},
...
]
And then you would group them in the following way:
r.table(...).group('property').count().ungroup()