0

(sorry,the question title is quite awkward) Hello,I have two table with data like this:

//friend_group table
{
    .....
    {
       "id":"user-me"//owner id 
       "FriendGroups":[
             {"id":"friendGroup_1","Members":["friend_1","friend_2"]},
             {"id":"friendGroup_2","Members":["friend_1","friend_3","friend_4"]},
        ]
    }
    .....
}

//friend table
{
    .....
    {"id":"friend_1","Skills":["java","c++"]}
    {"id":"friend_2","Skills":["python","ruby"]}
    {"id":"friend_3","Skills":["golang","c++"]}
    {"id":"friend_4","Skills":["javascript","ruby"]}
    ....
}

and I want to query out "user-me"'s friends who have been assigned to group "friendGroup-2" and have skill "c++",for given data result should be:

["friend_1","friend_3"]

and I have tried like this:

r.db("test").table("friend").filter(function(u){
    return r.table("friendGroup").get("user-me")("FriendGroups").filter(function(fg){
      return fg("id").eq("friendGroup_2").and(fg("Members").contains(u("id")))
})}).pluck("id")

What is the problem of my script?

Update my script is:

r.db("test").table("friend").filter(function(u){
    return r.table("friendGroup").get("user-me")("FriendGroups").filter(function(fg){
      return fg("id").eq("friendGroup_2").and(fg("Members").contains(u("id")))
})}).filter(function(u){return u("Skills").contains("skill-2")}).pluck("id").pluck("id")
Alex Luya
  • 9,412
  • 15
  • 59
  • 91

1 Answers1

0

It turns out that change out out return's "filter" to "contains" will solve this problem,right script should be:

r.db("test").table("friend").filter(function(u){
return r.table("friendGroup").get("user-me")("FriendGroups").**contains**(function(fg){
  return fg("id").eq("friendGroup_2").and(fg("Members").contains(u("id")))
})}).filter(function(u){return u("Skills").contains("skill-2")}).pluck("id").pluck("id")
Alex Luya
  • 9,412
  • 15
  • 59
  • 91