根据对象数组

时间:2018-06-07 02:10:55

标签: rethinkdb rethinkdb-python

RethinkDB newb在这里,我无法想出这个。

假设我有一个名为mydata的表,其中的文档具有以下基本结构:

{
    "SomeAttirbute": "SomeValue",
    "team": [
        {
            "name":  "john" ,
            "other": "stuff",
        } ,
        {
            "name":  "jane" ,
            "other": "junk",
        }
    ] ,
    ...
}

如何在mydata表格中获取john数据中任何元素的name属性值team的所有文档?< / p>

2 个答案:

答案 0 :(得分:0)

这非常简单,需要一个简单的ReQL表达式。在JavaScript中,它将是这样的:

const name = 'john';

...

r.db('q50732045')
  .table('mydata')
  // The predicate below can be literally read as:
  //     a document whose `team` property is a sequence
  //     that contains any element with a property `name`
  //     that equals the given name
  .filter(doc => doc('team').contains(member => member('name').eq(name)))
  // No need to invoke the run method in Data Explorer
  ;

我相信它可以很容易地用Python重写。

答案 1 :(得分:0)

我认为这就是您想要的:

r.db(insert_database_name).table("mydata").filter(
    lambda doc: doc["team"]["name"].contains("john")
).run(con)

或:

r.db(insert_database_name).table("mydata").filter(
    r.row["team"]["name"].contains("john")
).run(con)