如何从RethinkDB中的嵌入式数组中删除特定项?

Sam*_*l G 6 rethinkdb

给出样本数据,如:

{
   'id': 1,
   'things': [{'name': 'a'},{'name': 'b'},{'name': 'c'}]
}
Run Code Online (Sandbox Code Playgroud)

如何更新从嵌入式阵列中删除名称为"b"的数组项的文档?

r.table('test')
.get(1)
.update({things: r.row('things')????});
Run Code Online (Sandbox Code Playgroud)

Jor*_*lva 10

您可以使用该update命令filter来过滤数组中的元素并传递给update.

r.table('30848200').get(1).update(function (row)  {
  return {
    'things': row('things')
      .filter(function (item) { return item('name').ne('b') })
  }
})
Run Code Online (Sandbox Code Playgroud)

基本上,你将things用过滤后的数组覆盖.