RethinkDB Query: How to pluck bassed on a date range - rethinkdb

Given a table called alerts and a database called database with an array
of objects with a date attribute called History how can I pluck based
on a date range on that date attribute?
with the following query,
r.db("database").table("alerts").pluck("history").limit(10000)
I get back something like the following
{
"history": [
{
"text": "text1" ,
"updateTime": Thu Jun 20 2019 01:29:47 GMT+00:00 ,
},
{
"text": "text2" ,
"updateTime": Thu Jun 20 2019 01:24:59 GMT+00:00 ,
},
]
}
{
"history": [
{
"text": "text3" ,
"updateTime": Thu Jun 20 2018 01:29:47 GMT+00:00 ,
},
{
"text": "text4" ,
"updateTime": Thu Jun 20 2018 01:24:59 GMT+00:00 ,
},
]
}
how can I pluck the sub object called history and only return histories that are in a specific range on the updateTime attribute.
for example between jan/2/2009 to jan/3/2009

You need to filter based on a time range and use pluck on a nested object. Here are some examples about how to do that from the official documentation
r.table("users").filter(function (user) {
return user("subscriptionDate").during(
r.time(2012, 1, 1, 'Z'), r.time(2013, 1, 1, 'Z'));
}).run(conn, callback);
Source: https://www.rethinkdb.com/api/javascript/filter/
r.table('marvel').pluck({'abilities' : {'damage' : true, 'mana_cost' : true}, 'weapons' : true}).run(conn, callback)
Source: https://www.rethinkdb.com/api/javascript/pluck/

Related

Columns order of WebDataRocks pivot grid

Trying to build a grid with months as columns using webdatarocks, and the problem is that columns are sorted alphabetically (Apr 2020, Aug 2020, Dec 200, ...). Is there an option to order columns by date (Dec 200, Nov 2020, Oct 2020, ...)?
Example is available here
https://codesandbox.io/s/nifty-stonebraker-7mf56?file=/src/App.tsx
This is possible by adding an object to your data that will define data types. Here is an explanation.
In your case, this object would look this way:
{
"CONTRACT": {
"type": "string"
},
"value": {
"type": "number"
},
"date": {
"type": "date string"
},
"name": {
"type": "string"
}
}, {
type: "CONTRACT",
value: 217,
date: "Dec 2020",
name: "24"
}, {
type: "CONTRACT",
value: 725.84,
date: "Dec 2020",
name: "3 "
}, ...
After this, the columns should be ordered by dates. Note that input dates should be formatted properly (compliant with ISO 8601).
The way dates are shown inside WebDataRocks can be modified with the help of datePattern from options.

rethinkdb grouping to calculate balance of user funds

i am using rethinkdb with nodejs. i have a funds table and i am trying to calculate balance of any user by adding all the credit entries minus total debit entries. So far i was able to run following query.
r.db('testDB').table('funds').filter({userId:'63755d1e-e82e-4072-8312-4fcd88f1dfd3'}).group(function(g){
return g('userId')
})
this will produce following results.
[
{
"group": "63755d1e-e82e-4072-8312-4fcd88f1dfd3" ,
"reduction": [
{
"createdAt": Mon Jun 06 2016 14:17:26 GMT+00:00 ,
"createdBy": "63755d1e-e82e-4072-8312-4fcd88f1dfd3" ,
"credit": 900 ,
"id": "2afaca8e-6b4f-4ed5-a8ef-7fed3ce5ca67" ,
"userId": "63755d1e-e82e-4072-8312-4fcd88f1dfd3"
} ,
{
"createdAt": Fri Jun 17 2016 09:02:19 GMT+00:00 ,
"createdBy": "63755d1e-e82e-4072-8312-4fcd88f1dfd3" ,
"credit": 150 ,
"id": "c023ea2d-0d28-4f4b-ae6c-1c41c49aca08" ,
"userId": "63755d1e-e82e-4072-8312-4fcd88f1dfd3"
} ,
{
"createdAt": Fri Jun 17 2016 08:54:56 GMT+00:00 ,
"createdBy": "63755d1e-e82e-4072-8312-4fcd88f1dfd3" ,
"debit": 50 ,
"id": "89fd4a56-8722-4e86-8409-d42e4041e38d" ,
"userId": "63755d1e-e82e-4072-8312-4fcd88f1dfd3"
}
]
}
]
I have tried to use concatMap function and inside that tried using branch to check if its debit or credit but its not working.
this throwing errors
r.db('testDB').table('funds').filter({userId:'63755d1e-e82e-4072-8312-4fcd88f1dfd3'}).group(function(g){
return g('userId')
}).ungroup().concatMap(function(m){
//return m('reduction')('credit')
return r.branch (m('reduction')('credit').gt(0), 'c', 'd')
})
e: Cannot convert STRING to SEQUENCE in:
another approaching using reduce function provide me sum for all the credit entries but i dont know how to sum all debits.
r.db('testDB').table('funds').filter({userId:'63755d1e-e82e-4072-8312-4fcd88f1dfd3'}).group(function(g){
return g('userId')
}).ungroup().concatMap(function(m){
return m('reduction')('credit')
// return r.branch (m('reduction')('credit').gt(0), 'c', 'd')
})
.reduce(function(left, right){
return left.add(right);
})
result is 1050
You probably want something like this:
r.db('testDB').table('funds').group('userId').map(function(row) {
return row('credit').default(0).sub(row('debit').default(0));
}).sum()

sort nested timestamps in a query

I need to make a query with a list of Incidents and his nested events ordered DESC by his startedAt and timestamp dates. By default ReQL give the dates with a ASC order. I've got the folowing structure:
{
"id": "87e14db8-1e15-4718-baac-f1c785e985cb" ,
"title": "Connection Error"
"startedAt": Mon Oct 26 2015 14:33:00 GMT+00:00 ,
"events": [{
"message": "Cannot connect to theserver.com",
"timestamp": Mon Oct 26 2015 14:33:00 GMT+00:00
},{
"message": "Cannot connect to theserver.com,"
"timestamp": Mon Oct 26 2015 14:33:20 GMT+00:00
},{
"message": "Cannot connect to theserver.com",
"timestamp": Mon Oct 26 2015 14:33:40 GMT+00:00
}]
},{
"id": "87e14db8-1e15-4718-baac-f1c785e985cb" ,
"title": "Other Connection Error"
"startedAt": Mon Oct 26 2015 14:34:20 GMT+00:00 ,
"events": [{
"message": "Connection rejected",
"timestamp": Mon Oct 26 2015 14:34:20 GMT+00:00
},{
"message": "Connection rejected",
"timestamp": Mon Oct 26 2015 14:34:41 GMT+00:00
}]
},{
... (several more)
}
If I run r.db('mydb').table('Incident').orderBy(r.desc('createdAt')), the Incident's are ordered by createdAt as espected. But the nested eventsare still ordered ASC.
How can I make a query in order to get the nested events with a DESC order by timestamp?
Something like this should do it:
r.table('Incident').orderBy(r.desc('createdAt')).merge(function(row) {
return {events: row('events').orderBy(r.desc('timestamp'))};
})
I think this is what you're looking for. Just took a little wizardy with the .map(...) method.
r.db("test").table("stackoverflow").orderBy(r.desc('startedAt')).map(function(d){
return {
"startedAt":d("startedAt"),
"title": d("title"),
"id": d("id"),
"events": d("events").orderBy(r.desc("timestamp"))
}
})

aggregate data and still use changefeed in rethinkDB

I'm enjoying learning reQL so far but I stumbled upon a problem.
This is the data that I have stored in a table called events
[{
"date": "Tue Mar 17 2015 00:00:00 GMT+00:00" ,
"id": "00dacebd-b27e-49b5-be4b-42c2578db4bb" ,
"event_name": "View page" ,
"total": 4 ,
"unique": 4
},
{
"date": "Mon Mar 16 2015 00:00:00 GMT+00:00" ,
"id": "09ac3579-960b-4a2b-95be-8e018d683494" ,
"event_name": "View page" ,
"total": 68 ,
"unique": 35
},
{
"date": "Tue Mar 17 2015 00:00:00 GMT+00:00" ,
"id": "0bb01050-e93d-4845-94aa-b86b1198338d" ,
"event_name": "Click" ,
"total": 17 ,
"unique": 8
},
{
"date": "Mon Mar 16 2015 00:00:00 GMT+00:00" ,
"id": "174dcf3e-7c77-47b6-a05d-b875c9f7e563" ,
"event_name": "Click" ,
"total": 113 ,
"unique": 35
}]
And I would like the end result to look like this
[{
"date": "Mon Mar 16 2015 00:00:00 GMT+00:00",
"Click": 113,
"View Page": 68
},
{
"date": "Tue Mar 17 2015 00:00:00 GMT+00:00",
"Click": 17,
"View Page": 4
}]
The closet I got was with this query:
r.table("events").orderBy({index: r.desc('date')}).group('date').map(function(event) {
return r.object(event('repo_name'), event('unique'));
}).reduce(function(a, b) {
return a.merge(b.keys().map(function(key) {
return [key, a(key).default(0).add(b(key))];}).coerceTo('object'));
})
The results is:
[{
"date": "Mon Mar 16 2015 00:00:00 GMT+00:00",
"reduction": {
"Click": 113,
"View page": 68
}
},
{
"group": "Tue Mar 17 2015 00:00:00 GMT+00:00",
"reduction": {
"Click": 17,
"View page": 4
}
}]
However as you can see the events are nested under reduction and changefeeds won't work on this query either :(
Anyone can point me in the right direction?
Cheers,
You can change the group/reduction format like this:
query.ungroup().map(function(row){
return r.expr({date: row('group')}).merge(row('reduction'));
})
Unfortunately changefeeds on aggregations aren't supported as of 1.6, but that should be possible in 2.2 or 2.3 (so in a few months).

Count the different variables in an array in my document

I'm trying out rethinkDB and playing around with some query to see if it could fit by use case. So far, so good. However, I have a question regarding reQL.
For example in this case I store analytics events in rethinkDB such as:
[{
"userId": "abdf213",
"timestamp": "Sat Jan 17 2015 00:32:20 GMT+00:00",
"action": "Page"
},
{
"userId": "123abc",
"timestamp": "Sat Jan 17 2015 00:42:20 GMT+00:00",
"action": "Track"
},
{
"userId": "abdf213",
"timestamp": "Sat Jan 17 2015 00:45:20 GMT+00:00",
"action": "Track"
},
{
"userId": "123abc",
"timestamp": "Sat Jan 17 2015 00:44:20 GMT+00:00",
"action": "Page"
},
{
"userId": "123abc",
"timestamp": "Sat Jan 17 2015 00:48:20 GMT+00:00",
"action": "Page"
}]
I'd like the end result of my query to look like this:
{
"group": "123abc",
"reduction": {
"Page": 2,
"Track": 1
}
},
{
"group": "abdf213",
"reduction": {
"Page": 1,
"Track": 1
}
}
Bear in mind that the action name are not known in advance.
TBH, I'm not quite sure how to achieve this with ReQL.
Right now I have this query (using the data explorer):
r.db('test').table('events').group('userId').map(function(event) {
return event('action')
})
which return doc like this one:
{
"group": "-71omc5zdgdimpuveheqs6dvt5q6xlwenjg7m" ,
"reduction": [
"Identify" ,
"Page" ,
"Track"
]
}
Anyone can point me in the right direction here?
Cheers,
S
Try:
r.table('events').group('userId').map(function(event) {
return r.object(event('action'), 1);
}).reduce(function(a, b) {
return a.merge(b.keys().map(function(key) {
return [key, a(key).default(0).add(b(key))];}).coerceTo('object'));
})
Here's my solution:
r.table("events").group("userId", "action").count().ungroup()
.group(r.row("group")(0))
.map([r.row("group")(1), r.row("reduction")])
.coerceTo("object")
ReQL doesn't support nesting groups, but you can group by multiple fields at the same time and then performing further grouping on the output.

Resources