如何从RethinkDB文档中删除密钥?

问题描述 投票:39回答:3

我正在尝试从RethinkDB文档中删除密钥。我的方法(没有用):

r.db('db').table('user').replace(function(row){delete row["key"]; return row}) 

其他方法:

r.db('db').table('user').update({key: null}) 

这个只设置row.key = null(看起来合理)。

通过Web UI在rethinkdb数据资源管理器上测试的示例。

rethinkdb rethinkdb-python
3个回答
71
投票

以下是RethinkDB网站上的相关示例:http://rethinkdb.com/docs/cookbook/python/#removing-a-field-from-a-document

要从表中的所有文档中删除字段,您需要使用replace将文档更新为不包含所需字段(使用without):

r.db('db').table('user').replace(r.row.without('key'))

要从表中的一个特定文档中删除该字段:

r.db('db').table('user').get('id').replace(r.row.without('key'))

您可以使用API​​中的任何选择器(http://rethinkdb.com/api/)更改要更新的文档选择,例如: dbtablegetget_allbetweenfilter


11
投票

你可以使用replacewithout

r.db('db').table('user').replace(r.row.without('key'))

2
投票

您无需使用replace来更新整个文档。以下是相关文档:ReQL command: literal

假设您的用户文档如下所示:

{
  "id": 1,
  "name": "Alice",
  "data": {
    "age": 19,
    "city": "Dallas",
    "job": "Engineer"
  }
}

并且您想要从数据属性中删除年龄。通常,更新只会将您的新数据与旧数据合并。 r.literal可用于将数据对象视为单个单元。

r.table('users').get(1).update({ data: r.literal({ age: 19, job: 'Engineer' }) }).run(conn, callback)

// Result passed to callback
{
  "id": 1,
  "name": "Alice",
  "data": {
    "age": 19,
    "job": "Engineer"
  }
}

要么

r.table('users').get(1).update({ data: { city: r.literal() } }).run(conn, callback)

// Result passed to callback
{
  "id": 1,
  "name": "Alice",
  "data": {
    "age": 19,
    "job": "Engineer"
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.