简体   繁体   中英

How to apply Greater than equal in python lambda expression in Rethinkdb

I have below json record in RethinkDB table.

[{
"pid": 12,
 "sk": [
       {
        "sid": 30,
        "et": 3
       },
       {
        "sid": 22,
        "et": 10
       },
       {
        "sid": 30,
        "et": 8
       }
      ],
"wc": [
      {
       "wid": 7,
       "et": 8
      },
      {
       "wid": 3,
       "et": 6
      },
      {
       "wid": 9,
       "et": 7
      }
    ]
}]

Like this one, I have millions of rows in the table. What am trying to achieve is to filter this json based on input sets of {sid,et}

Am using below code in python (skObj is the input) ::

skObj=[{'sid': 1, 'et': 9},{'sid': 27, 'et': 6}]
cursor2=r.table('cube7').filter(lambda row: r.expr(skObj).set_difference(row['sk']).is_empty())['pid'].run(t)
cur_list2 = list(cursor2)

The Above query correctly filters my cube7 table in RethinkDB as per the input sets of sk. skObj can contain sets upto 10.

What I would like to see is for every input set

skObj=[{'sid': 22, 'et': 10},{'sid': 30, 'et': 8}]

I would like to filter the table with this condition:

(sid=22 & et>=10) and (sid=30 & et>=8)

But currently it is doing equals only like

(sid=22 & et=10) and (sid=30 & et=8)

How can I use greater than inside my lambda expression for et values for each set of (sid,et) ?

How can I create generic expression from below - this works with raw data

lambda x: (x['sid'] == 22) & (x['et'] >= 10)

So you want to get all the documents where the sk array contains at least one document matching each predicate?

Does this do what you want?

r.table('cube7').filter(
  lambda row: r.and_(r.args(r.expr(skObj).map(
    lambda x: row['sk'].contains(
      lambda y: (y['sid'] == x['sid']) & (y['et'] >= x['et'])
    )
  )))
)

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