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

…衆ロ難τιáo~ 提交于 2019-12-25 16:54:01

问题


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)

回答1:


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'])
    )
  )))
)


来源:https://stackoverflow.com/questions/33977395/how-to-apply-greater-than-equal-in-python-lambda-expression-in-rethinkdb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!