简体   繁体   中英

How to get data from Rethinkdb table based on field name instead of field value

I have a rethinkdb table which have 5 rows, one of the row is following:

{
"appkey": {
"YcJ1HR6vjebXNHwOzeC2l2EAvUNw8qyp": {
"createdBy": {
"fullName":  "DD" ,
"id":  "7943d176-4805-461d-841e-3de766a3825d" ,
"primaryName": dd@gmail.com, »
} ,
"creationTime":  "2016-01-20T05:57:40.773+00:00" ,
"expiryTime":  "2017-01-20T05:57:40.539+00:00"
}
} ,
"creationTime":  "2016-01-20T05:57:40.773+00:00" ,
"customerId":  "U2KRpPbK" ,
"domain":  "co.in" ,
"id":  "40e536cc-08f1-4a54-8104-13d900abd643" ,
"kind":  "admin#option1#option2" ,
"roles": {
"admin#option1#option2": {
"create": true ,
"delete": true ,
"modify": true ,
"read": true
} 
}
}
}

In this case how can I fetch above row based on the appKey's field "YcJ1HR6vjebXNHwOzeC2l2EAvUNw8qyp", which is token that I am getting form client end.

To do the same I tried with following query:

r.db('admin').table('services')
        .filter(function (record) {  
                return record('appkey').coerceTo('array')
                .map(function (record) {
                    return record(1).hasFields(token)
                });
                .distinct()

        })

        .run()

But above query returning only 1 row in every case whether the token is for 4 row or 2 row.

What I am doing wrong!

I want to expand Anders Bornholm 's answer on hasFields because hasFields is very powerful. When applying on a sequence, it can act like a filter.

You query can be just simple as:

r.db('admin').table('services')
  .hasFields({'appkey': {'YcJ1HR6vjebXNHwOzeC2l2EAvUNw8qyp': true}})

Using the nested field syntax we can write very easily powerful query when it contains many deep down level of fields.

You can use hasFields directly in the filter:

r.db('admin').table('services')
   .filter(function (service) 
       { return service('appkey').hasFields('YcJ1HR6vjebXNHwOzeC2l2EAvUNw8qyp') })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM