RethinkDB:​​检查数组中的对象是否存在并替换它

RethinkDB: Check if object in array exist and replace it

我有这个 JSON 文档:

{
  userId: xx,
  followedAuthors: [
    { authorId: abc, timestamp: 123 },
    { authorId: xyz, timestamp: 456 },
  ]
}

当用户想要关注某个作者时,我想编写一个查询来检查该作者是否已被关注,检查 id,如果没有,则将新关注的作者追加到数组中。

现在我每次都创建一个新条目。 这是我的查询:

r.table('users')
 .get(userId)
 .replace(user => {
   return user.merge({
     followedTopics: user('followedTopics')
        .default([])
        .setInsert({ topic: topic, timestamp: now }),
     })
  })

实现这一点的最佳方法是使用 contains(使用谓词函数)、branchappend.