How to Join with multiple conditions and use aggregate function in Rethink-db
r.table("yourtable").get(id).innerJoin(
r.table("secondtable"),function (up, upp) {
return user_profile("mID")
}).map({
uId: r.row("left")("userId"),
mId: r.row("left")("memberId"),
pId: r.row("right")("projectId"),
nodeId: r.row("right")("nodeId"),
pri: r.row("right")("priority")
})
.min("priority");
r.table("tblName1").get(id).innerJoin(
r.table("tblName2"),
function (tbl1, tbl2) {
return tbl1("userId").eq(tbl2("userid"));
})
You can try below one where tblName1 Joins with tblName2. Having userId as mapping key. Note its a innerJoin.
r.table("tblName1").get(id).innerJoin(
r.table("tblName2"),
function (tbl1, tbl2) {
return tbl1("userId").eq(tbl2("userid"));
}).zip()
Also you can check a reference of all the sql to reql here.
Hope it helps :)
.table("tblName1").innerJoin(
r.table("tblName2"), function(tl, path){
return tblName1("value").eq(path("value"));
}
).map({nodeName:r.row("right")("value")
})
Related
I've been playing around with resolvers in graphql and need what's seemingly a simple query, but I can't figure it out.
I want to query a table and get results in something like this:
SELECT hero_id, count(id) FROM "Build"
GROUP BY hero_id
ORDER BY hero_id
How do I write the resolver to return the count of rows by id on a table?
I thought the Table.findAndCountAll() would return results I'm looking for.
const buildCount = {
type: BuildCountType,
resolve(parent, args){
return Build.findAndCountAll().then(result => {
console.log(result)
return {
count: result.rows
}
})
}
}
Thanks,
StuckAndConfused ;)
I have a document schema like this:
{
"name":"",
"type":"",
"posts":[
{
"url":"",
"content":"",
...
},
{
"url":"",
"content":"",
...
}
...
]
}...
I forgot to create id's for each post on insertion in database. So i'm trying to create a query for that:
r.db('test').table('crawlerNovels').filter(function (x){
return x.keys().contains('chapters')
}).map(function (x){
return x('chapters')
}).map(
function(x){
return x.merge({id:r.uuid()})
}
)
instead this query return all posts with an id but doesn't actually update in the database. I tried using a forEach instead of a map function at the end this doesn't work
After lots of tweaking and frustration i figured it out:
r.db('test').table('crawlerNovels').filter(function (x){
return x.keys().contains('chapters')
}).update(function(novel){
return {"chapters":novel('chapters').map(
function(chapter){
return chapter.merge({"id":r.uuid()})
})}
},{nonAtomic:true})
Rethinkdb docs has this example to improve getAll/contains queries with a secondary index:
// Create the index
r.table("users").indexCreate("userEquipment", function(user) {
return user("equipment").map(function(equipment) {
return [ user("id"), equipment ];
});
}, {multi: true}).run(conn, callback);
// Query equivalent to:
// r.table("users").getAll(1).filter(function (user) {
// return user("equipment").contains("tent");
// });
r.table("users").getAll([1, "tent"], {index: "userEquipment"}).distinct().run(conn, callback);
My questions is if there's a way to do the same but for querying with multiple tags. What would be the equivalent to make this query possible with a secondary index?
r.table("users").getAll(1).filter(function (user) {
return user("equipment").contains("tent", "tent2");
});
Probably we can do this
r.table("users").getAll([1, "tent"]).filter(function (user) {
return user("equipment").contains("tent2");
});
So build a multi index as you did, and try to getAll first, so that part is efficient with index, then filter to continue ensure that equipment contains array we want.
I have the following query that works fine but is slow, however I can't figure out how to index it properly:
r.db('my_db')
.table('messages')
.filter({ community_id : community.id})
.filter(function(row){
return row('mentions').contains(user.id);
})
.filter(function(row){
return row('channels').contains(channel.id);
})
.orderBy(r.desc('created_at'))
.skip(0)
.limit(50);
I tried with the following index (using Thinky.js):
Model.ensureIndex("user_mentions", function(message){
return message("mentions").map(function(user_id){
return message("channels").map(function(channel_id){
return [
message("community_id"),
message("mentions").contains(user_id),
message("channels").contains(channel_id),
message('created_at')
];
});
});
}, {multi: true});
And then to query it I've tried this:
r.db('my_db')
.table('messages')
.between(
[community.id, user.id, channel.id, r.minval],
[community.id, data.user.id, channel.id, r.maxval],
{ index : 'user_mentions' }
)
.orderBy({index:r.desc("user_mentions")})
.skip(0)
.limit(50);
The messages table looks like:
id | community_id | mentions (array of user_ids) | channels (array of channel_ids) | created_at
But I end up getting zero results.
I greatly appreciate any suggestions!
I think this index will make the between query you wrote above work:
.indexCreate(function(message) {
return message('channels').concatMap(function(channel) {
return message('mentions').map(function(mention) {
return [
message('community_id'),
mention,
channel,
message('created_at')
];
});
});
}, {multi: true});
Given a example like
r .table('posts')
.get(100)
.merge(function (post) {
return {
comments: r.table('comments').getAll(post('id'),
{index: 'postId'}).coerceTo('array')
}
})
.pluck({"comments": ["id", "created_on"]});
How do I further filter comments to return only comments by a particular user on the given blog post. Ie. Get blog post 100 and return its comments by user_name == 'darth'
Tips greatly appreciated
Finally went with the following:
r
.table('posts')
.get(100)
.merge(function (post) {
return {
comments: r
.table('comments')
.getAll(post('id'), {index: 'postId'})
.coerceTo('array')
.filter(function (row) {
return r.expr(['darth', 'luke', 'chewey']).contains(row('user_name'));
})
}
})
.pluck({"comments": ["id", "user_name", "created_on"]});